IOS development practices: xib loading considerations

Source: Internet
Author: User

IOS development practices: xib loading considerations
When xib adds addSubview to the Controller view, the program crashes. Error message:

'Nsinvalidargumentexception ', reason :'-[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x7ffae1c1b680'

The error is obviously not found.[UITapGestureRecognizer superview] method.

Check the problematic code:

- (void)viewDidLoad {    [super viewDidLoad];        ToolBar *toolBar = [ToolBar toolBar];    [self.view addSubview:toolBar];}
# Import "ToolBar. h "@ implementation ToolBar // return xib view + (instancetype) toolBar {NSArray * arr = [[NSBundle mainBundle] loadNibNamed: @" ToolBar "owner: nil options: nil]; return [arr lastObject];} @ end
Looking at the code, we want to return a ToolBar object. How does one return UITapGestureRecognizer ???

In the toolBar method, print the array of the ToolBar in the bundle and find two objects. And the first one is not what we want TooBar.

Let's take a look at xib. There are actually two objects.

Conclusion: [arr lastObject] can be basically determined. The returned result is not what we want. Change to [arr firstObject] to solve the problem. Therefore, in the future, xib will return the array, which is what we want.

Ps: xib objects are arranged in the order they are put in. The order they are viewed is not necessarily the order of objects in the xib file.

Related Article

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.