iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

來源:互聯網
上載者:User

標籤:

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

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

If you are only dealing with UIViewController, you might have heard that a convenient method is could pass nil to initWithNibName:bundle: like this:

?
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 is the case, then this code will work fine. The reason is that when nil is passed to the method, a search is done to try to find the matching nib file for the view controller to load. Some people say this is a reasonable way to load the nib file. I would recommend new developers to the iOS to stay away from depending on this behavior and be explicit about which nib file to load. Otherwise, you would run into this next problem.

UITableViewController is another commonly used class of iOS. It’s a subclass of the UIViewController. If you try to do 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 way, whatever you customized in the Interface Builder for your table view would not show up when you run the program. The reason is that UITableViewController behaves differently when nil is pass toinitWithNibName:bundle:. It actually creates a new table view for the table view controller rather than try to search for the nib file. UITableViewController will only load from nib file if you specify the proper nib name. I don’t know what the rational behind it is, but the only consistent way to have the view controllers work properly is 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 two classes are 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 controller class. (In this latter case, the class name becomes the name stored in the nibName property.) If you do none of these, the view controller will be unable to load its view.

UITableViewController

If a nib file is specified via the initWithNibName:bundle: method (which is 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,UITableViewController, UIViewController About Louis Feng

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

iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.