IOS development:proper use of initWithNibName:bundle:Affects Uitableviewcontroller

Source: Internet
Author: User

address:http://www.outofcore.com/2011/07/ ios-development-proper-use-of-initwithnibnamebundle-affects-uitableviewcontroller/

I think this post is useful to someone new to the IOS development. One of the most important and commonly used classes in Cocoa are UIViewController . Creating any non-trivial application would involve subclass and Creating your own view controller. There is a lot of topics and tutorials elsewhere, so I'm not going to repeat them here. But I think one important concept this seems to being overlooked is what to initialize the view controller from a nib bundle.

If you were only dealing UIViewController with, you might had heard that a convenient method was could pass to like this nil initWithNibName:bundle: :

?
1 customviewcontroller* customviewcontroller = [[Customviewcontroller alloc] Initwithnibname: nil bundle: Nil

If the nib name is the same as the view controller (i.e. CustomViewController.xib ), which often are the case and then this code would work fine. nilthe reason is, and is passed to the method, a search was done to try to find the matching nib file for the view Controller to load. Some people say this is a reasonable-to-load the nib file. I would recommend new developers to the IOS to stay away from depending on this behavior and is explicit about which nib F Ile to load. Otherwise, you would run to this next problem.

UITableViewControlleris another commonly used class of IOS. It ' s a subclass of the UIViewController . If you try the similar thing as what's done above, you would run into some weird behavior.

?
1 customtableviewcontroller* customtableviewcontroller = [[Customtableviewcontrolleralloc] Initwithnibname: nil bundle: nil ]

If You initialize the table view controller this is whatever you customized in the Interface Builder for your table view Would not show up when you run the program. The reason is this UITableViewController behaves differently when is nil pass to initWithNibName:bundle: . It actually creates a new table view for the table view controller rather than try to search for the nib file. Would only UITableViewController load from nib file if you specify the proper nib name. I don ' t know what's the rational behind it is, but the only consistent-on-the-controllers work properly are to Specify the nib name if you want to load them from a nib file:

?
1 CustomTableViewController* customTableViewController = [[CustomTableViewControlleralloc] initWithNibName:@"CustomTableViewController"bundle:nil]

The details (and differences) of Init from NIB for these, classes is specified in their class reference documentation.

UIViewController

If you specify nil for the Nibname parameter, you must either override the Loadview method and create your views there or You must provide a nib file in your bundle whose name (without the. nib extension) matches the name of your view Controlle R class. (In this latter case, the class name becomes the name stored in the Nibname property.) If you do none of the these, the view controller is unable to load its view.

UITableViewController

If a nib file is specified via the InitWithNibName:bundle:method (which was declared by the superclass Uiviewcontroller), Uitableviewcontroller loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the TableView property.

Filed Under:blog, Code, Mobile Tagged With:cocoa, IOS, ipad, iphone, nib, software Engineering,uitableviewcontro Ller, Uiviewcontroller About Louis Feng

I have been a computer graphics enthusiast and researcher for many years. My interests have broadened to include mobile, high performance computing, machine learning, and computer vision.

IOS development:proper use of initWithNibName:bundle:Affects Uitableviewcontroller

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.