0
votes

My app crashes when adding Gesture to Custom View (XIB). I am using Xcode version 6.4.

Below are the steps I followed to add a custom subview with tap gesture:

  1. Added an XIB and a UIView subclass (MyView) to my project. And set the XIB class to MyView.

  2. Added a TapGesture to MyView using Interface Builder

  3. Created MyView object (myView) and added it as a sub view using [addSubview:myView].

  4. When I run the app, it crashes

  5. Removed the TapGesture in XIB and run again with no issues.

Code:

[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] firstObject];

Log:

-[UITapGestureRecognizer setFrame:]: unrecognized selector sent to instance 0x7f924a4ce910

Sometimes like this,

-[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x7f924a4ce910

Please advice.

1
do you have some code in awakeFromNib or drawRect of your MyView implementation class. it seems like you are trying to set frame on TapGesture.Pawan Rai
Yes awakeFromNib is there. But not setting any frames there.user4226071
you can try to add you tapgesture in awakeFromNib of you MyView. just to check if something going wrong in xib.Pawan Rai
It works when adding gesture using code. But I wanted to know what is wrong with my code/xib.user4226071
@pawan: thanx for your advice. I have solved the issue by adding some extra line of code. I will add answer to this question.user4226071

1 Answers

0
votes

I solved the issue by replacing the code,

[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] firstObject];

with

MyView *myView = nil;
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
for (id object in objects)
{
  if ([object isKindOfClass:[MyView class]])
  {
    myView = object;
  }
}

Note:

If the XIB have a single UIView object only, then the single line code mentioned above is enough. But if the XIB have multiple UIView objects or any Gestures added in XIB, then we need to find out the MyView object from the array returned by the loadNibNamed:owner:options: method.