Reputation:
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:
Added an XIB and a UIView subclass (MyView) to my project. And set the XIB class to MyView.
Added a TapGesture to MyView using Interface Builder
Created MyView object (myView) and added it as a sub view using [addSubview:myView]
.
When I run the app, it crashes
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
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