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.

iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController的更多相关文章

  1. iOS 初始化(init、initWithNibName、initWithCoder、initWithFrame)

    很多朋友如果是初学iOS开发,可能会被其中的几个加载方法给搞得晕头转向的,但是这几个方法又是作为iOS程序员必须要我们掌握的方法,下面我将对这几个方法做一下分析和对比,看看能不能增加大家对几个方法的理 ...

  2. initWithNibName:bundle awakeFromNib 区别

    initWithNibName:bundle 定义:is a message sent to a view (or window) controller in order to create the ...

  3. iOS Development Sites

    iOS Development Sites   学习iOS开发有一段时间了,虽然还处于迷茫期,但相比以前的小白痴状态,现在还是蛮有改观的.期间接触了一些很好的网站和博客,现在摘录下来,就当建个索引,没 ...

  4. Setup iOS Development Environment.

    Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...

  5. iOS 封装SDK以及封装时bundle文件的处理

    这篇教程的主要目的是解释怎么样在你的iOS工程中创建并使用一个SDK,俗称.a文件. 环境:xcode 9.0 创建一个静态库工程 打开Xcode,点击File\New\Project, 选择iOS\ ...

  6. iOS和OS X中的bundle

    bundle也可以称之为包(package). 它在iOS和OS X中实际为一个文件夹但却当成单独的文件来对待. 每一个app都有一个bundle,并且你可以通过在xxx.app图标上右击鼠标然后选择 ...

  7. iOS Development和iOS Distribution有什么区别

    http://zhidao.baidu.com/link?url=T9od33JuA7jjxzfyV-wOjuVLSNUaqpc9aoCu2HjfYfHBuRLW1CNDii0Bh9uvG6h-GeJ ...

  8. iOS Development Learning 13Nov

    关注了关东升老师在博客园的iOS开发博客. 在使用能力课堂观看智捷课堂的iOS8开发视频教程.观看到Part1 课时3 Xcode中的iOS工程模板

  9. iOS - 获取安装所有App的Bundle ID

    先导入#import <objc/runtime.h>头文件 使用runtime获取设备上的所有app的bundle id // Class LSApplicationWorkspace_ ...

随机推荐

  1. html的显示消息和留言板

    <div class="inner_content"> <c:forEach items="${notices}" var="n&q ...

  2. R语言学习之主成分分析法的R实践

    主成分分析R软件实现程序(一): >d=read.table("clipboard",header=T) #从剪贴板读取数据 >sd=scale(d)  #对数据进行标 ...

  3. Python学习之路——初识Python

    一.第一个程序Hello World: 1.打印输出Hello World: Python2打印方法: >>> print "hello world"hello ...

  4. [LeetCode]题解(python):076-Minimum Window Substring

    题目来源: https://leetcode.com/problems/minimum-window-substring/ 题意分析: 给定两个字符串S和T.在S中找到最短的一个子字符串使得他包括所有 ...

  5. 模拟美萍加密狗--Rockey2虚拟狗(四)

    目录(?)[+]       首先,抱怨一下.学校个破网,似乎把我端口封了,死活分不上IP,也许是是我MAC改的太频繁了,有盗号嫌疑…… 然后,正文开始…… 其实虚拟狗几天前就写完了,可这几天上不了网 ...

  6. FastJson中@JSONField注解使用

    最近做项目中,使用了json格式在服务器之间进行数据传输.但是发现json格式数据不符合JAVA中的变量定义规则,并且难以理解,因此需要在后台中做二次处理,将数据处理成我们系统中定义的格式. 思路: ...

  7. POJ 1655 - Balancing Act 树型DP

    这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...

  8. fopen()函数

    1.2 文件的输入输出函数 键盘.显示器.打印机.磁盘驱动器等逻辑设备, 其输入输出都能够通过文件管理的方法来完毕.而在编程时使用最多的要算是磁盘文件, 因此本节主要以磁盘文件为主, 具体介绍Turb ...

  9. Javabean的理解

    1.Javabean是指满足指定规则的Java类.满足的规则由Sun公司提出,如x,setX,getX等规范,public无参构造函数等 2.Javabean其实也是一种规范.

  10. URAL 1225 Flags

    题目:click here #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int n; ll ...