zoukankan      html  css  js  c++  java
  • iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

    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.

    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.

    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    数据仓库系列之维度建模
    为什么数据分析中要建数据仓库?
    C1WPF制作OLAP Cube浏览工具
    WPF自学入门(十二)WPF MVVM模式提取函数
    WPF自学入门(十一)WPF MVVM模式Command命令
    WPF自学入门(十)WPF MVVM简单介绍
    WPF自学入门(九)WPF自定义窗口基类
    大话Git系列之初识版本控制系统(1)
    C#(1)运用C#实现一键从Word文档转换TXT文本的功能
    arcgis api for javascript 学习(六) 地图打印
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4531514.html
Copyright © 2011-2022 走看看