IOS---loadview usage scenarios in Uiviewcontroller

Source: Internet
Author: User

Problem

Look at the code first, create a new subclass of Uiviewcontroller Testviewcontroller (including the nib file), Imageviewcourse and Lbcourse are its two properties, added by the Iboutlet way of nib.

TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];testViewController.imageViewCourse.image = image;testViewController.lbCourse.text = course;[self.navigationController presentViewController:testViewController animated:YES completion:nil];

We use NIB to load a testviewcontroller and assign values to its properties, and then jump. The problem is that after the execution of Initwithnibname, Testviewcontroller.imageviewcourse and Testviewcontroller.lbcourse are nil, It shows that after jumping to testviewcontroller, there is no content in Imageviewcourse and Lbcourse.

Workaround

The

uses the Loadview method to trigger the loading of uiview in the nib.

@property(null_resettable, nonatomic,strong) UIView *view; // The getter first invokes [self loadView] if the view hasn‘t been set yet. Subclasses must call super if they override the setter or getter.- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren‘t using a nib. Should never be called directly.- (void)loadViewIfNeeded NS_AVAILABLE_IOS(9_0); // Loads the view controller‘s view if it has not already been set.

That

TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];//[testViewController view];//[testViewController loadView];[testViewController loadViewIfNeeded];testViewController.imageViewCourse.image = image;testViewController.lbCourse.text = course;[self.navigationController presentViewController:testViewController animated:YES completion:nil];

Code as above, do not explain too much. Three different ways in fact.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS---loadview usage scenarios in Uiviewcontroller

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.