What to pay attention to when loading xib files with gesture recognizers _ios

Source: Internet
Author: User

If you need to detect gestures in your application, such as tap (tap), pinch, drag (pan), and rotate (rotation), it is easy to create a Uigesturerecognizer class.

Next, we'll share the main problem of loading the Xib file with the gesture recognizer.

Start by creating a new class that inherits from UIView and create the corresponding xib file

#import <UIKit/UIKit.h>
@interface zhdemoview:uiview
+ (instancetype) DemoView;
@end

Provides a class method for quickly creating the current class

Implementing a class method in. m, usually loaded from the xib is an array, so the Lastobject method is used

+ (instancetype) demoView
{
 Nsarray *nibarray = [[NSBundle mainbundle] loadnibnamed:@ "Zhdemoview" Owner:nil Options:nil];
 return [Nibarray Lastobject];
}
Add the corresponding gesture in the awakefromnib
-(void) awakefromnib
{
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer Alloc] initwithtarget:self Action: @selector (click)];
 [Self Addgesturerecognizer:tap];
}
-(void) Click
{
 NSLog (@ "%s", __func__);
}

In addition, the xib length of the test is this:

Add one such view to the root controller, run and click

The result is

2015-08-11 13:44:44.988 XIB

Attention

[1797:362096]-[zhdemoview Click]

Everything's fine, and then we don't add gestures to the code, just add the Xib (the previously added gesture code is cleared):

Run Result:

2015-08-11 14:02:12.747 XIB

Attention

[1962:374487]-[uitapgesturerecognizer Superview]: Unrecognized selector sent to instance 0X7FE47AF49C60

You can see that there is a message sending error and there is no Superview method in UITapGestureRecognizer.

Back to this picture

We can see that there is a gesture recognizer and demoview two objects underneath the objects bar, is it possible that the Xib loaded object is wrong?

Because we used the Lastobject method in the previously provided class method, this time we use Firstobject to try

Change the class method as follows:

+ (instancetype) demoView
{
 Nsarray *nibarray = [[NSBundle mainbundle] loadnibnamed:@ "Zhdemoview" Owner:nil Options:nil];
 return [Nibarray Firstobject];
}

Results:

2015-08-11 14:13:16.847 XIB

Attention

[2064:382058]-[zhdemoview click:]

Everything's fine.

Summarize:

An array of objects is loaded from the xib, and usually we just need to use one object, either Firstobject or Lastobject. But when you want to use the gesture recognizer, do not mistake the object you removed. It is recommended that you do not add a gesture recognizer directly to the xib, and that it is more secure and easier to maintain through code creation.

The above is the introduction of the load with gesture recognizer Xib file to pay attention to the problem, I hope to be able to help everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.