user4226071
user4226071

Reputation:

iOS App crashes with message "-[UITapGestureRecognizer setFrame:]" when adding Gesture in XIB

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.

Upvotes: 0

Views: 437

Answers (1)

user4226071
user4226071

Reputation:

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.

Upvotes: 0

Related Questions