0
votes

I tried to use initWithFrame but I am not able to display the UIView, This kind of code solved many questions as I saw here but not in my case:

- (id)initWithFrame:(CGRect)frame myParams:(NSArray*)params;

.m file


    - (id)initWithFrame:(CGRect)frame myParams:(NSArray*)params{
      self = [super initWithFrame:frame];
      if(self){
        UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
        //Trying to send an uiview with gesture include
        view.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] 
        initWithTarget:self action:@selector(test)];
        [view addGestureRecognizer:tap];    
        [self addSubview:view];
      }
      return self;
    }

Here is how I am trying to add the view in UIViewController: Maybe here I am doing it wrong, right?



    Custom *view = [Custom alloc] initWithFrame:self.view.bounds myParams:array];
      [self.view addSubview:view];

So, my question is it possible to add UITapGesture in the UIView generated in this class, cause it is not firing:

.h file


    + (UIView *)viewParams:(NSArray*)params;

.m file


    + (UIView *)viewWithParams:(NSArray*)params{
      UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];

      NSLog(@"read params %@", params);

      //Trying to send an uiview with gesture include
      view.userInteractionEnabled = YES;
      UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] 
      initWithTarget:self action:@selector(test)];
      [view addGestureRecognizer:tap];

      return view;
    }
    - (void)test{
      NSLog('it works...');
    }

UPDATE : As instance method is working. But as class method I can not make it work. somebody know if possible?

2
Can you try giving self.view.userInteractionEnabled = YES; in view controller?Arun
That did not work neither trying as class method.usosystem

2 Answers

0
votes

1st Part

If you are using XIB for your customView then you need to use awakeFromNib method. But if you are trying to create through code only then just use initWithFrame: method and add your gesture recognizer in the initializer.

2nd Part

For firing tap gesture recognizer you are adding recognizer target self i.e your customView. So event will fire in your customViewClass class only then to get these events in your controller you need to use protocol and delegate. And set the delegate of customView to whatever controller you want to use in the controller.

0
votes

View will add after firing the initwithcoder just use this code in init with coder ...

   dispatch_after(0.1, dispatch_get_main_queue(), ^(void)
           {
               UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
            //Trying to send an uiview with gesture include
            view.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(test)];
            [view addGestureRecognizer:tap];    
            [self addSubview:view];
           });
 }
      return self;
    }

Minimum Time interval 0.1 max time interaval set as per your data load requirement