iOS Development: Proper Use of initWithNibName:bundle: 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.
UIViewControllerIf 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.
UITableViewControllerIf 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.
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的更多相关文章
- iOS 初始化(init、initWithNibName、initWithCoder、initWithFrame)
很多朋友如果是初学iOS开发,可能会被其中的几个加载方法给搞得晕头转向的,但是这几个方法又是作为iOS程序员必须要我们掌握的方法,下面我将对这几个方法做一下分析和对比,看看能不能增加大家对几个方法的理 ...
- initWithNibName:bundle awakeFromNib 区别
initWithNibName:bundle 定义:is a message sent to a view (or window) controller in order to create the ...
- iOS Development Sites
iOS Development Sites 学习iOS开发有一段时间了,虽然还处于迷茫期,但相比以前的小白痴状态,现在还是蛮有改观的.期间接触了一些很好的网站和博客,现在摘录下来,就当建个索引,没 ...
- Setup iOS Development Environment.
Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...
- iOS 封装SDK以及封装时bundle文件的处理
这篇教程的主要目的是解释怎么样在你的iOS工程中创建并使用一个SDK,俗称.a文件. 环境:xcode 9.0 创建一个静态库工程 打开Xcode,点击File\New\Project, 选择iOS\ ...
- iOS和OS X中的bundle
bundle也可以称之为包(package). 它在iOS和OS X中实际为一个文件夹但却当成单独的文件来对待. 每一个app都有一个bundle,并且你可以通过在xxx.app图标上右击鼠标然后选择 ...
- iOS Development和iOS Distribution有什么区别
http://zhidao.baidu.com/link?url=T9od33JuA7jjxzfyV-wOjuVLSNUaqpc9aoCu2HjfYfHBuRLW1CNDii0Bh9uvG6h-GeJ ...
- iOS Development Learning 13Nov
关注了关东升老师在博客园的iOS开发博客. 在使用能力课堂观看智捷课堂的iOS8开发视频教程.观看到Part1 课时3 Xcode中的iOS工程模板
- iOS - 获取安装所有App的Bundle ID
先导入#import <objc/runtime.h>头文件 使用runtime获取设备上的所有app的bundle id // Class LSApplicationWorkspace_ ...
随机推荐
- SQL Server 取前一天的0点和23点59分59秒
DECLARE @startDate1 DATE; DECLARE @startDate DATETIME; ,@startDate1); ,CONVERT(DATETIME,@startDate1) ...
- python 以面向对象的方式创建线程 实现售票系统
---恢复内容开始--- 转载或借鉴请注明转自http://www.cnblogs.com/FG123/p/5068556.html 谢谢! 通过面向对象的方法实现多线程,其核心是继承thread ...
- c语言(3)--运算符&表达式&语句
计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * / % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...
- 磁盘IO子系统学习资料
1.http://www.ibm.com/developerworks/cn/linux/l-cn-read/ (IBM read系统调用剖析) 2.http://lenky.info/arch ...
- 从一个App跳转到另一个App
在跳入App的info中配置Bundle identifier 在跳入App的info中配置URL Schemes 在另一个应用程序中按照上边的操作添加openURL并运行,就可以跳转了 调用open ...
- 飘逸的python - 一个最简单的服务器
python拥有这种单独起一个服务器监听端口的能力,用标准库的wsgiref就行. from wsgiref.simple_server import make_server def simple_a ...
- 面试之ajax原理(转载)
总结1 总结2 AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术, 是几种原有技术的结合体. ...
- MemberShip使用大全
比较全面的membership 使用过程 http://www.cnblogs.com/xlb2000/archive/2010/07/31/1788120.html Membership中创建的数据 ...
- eclipse修改java代码后报错: java.lang.OutOfMemoryError: PermGen space
由于在eclipse中运行项目后,我们又重新修改了某个java类,导致tomcat会重新加载这个项目所有的class.jar,多次加载后由于分配的存储空间有限,就导致了:java.lang.OutOf ...
- ACE6.2.0文件/目录操作
文件读取.#include "ace/FILE_Connector.h"#include "ace/FILE_IO.h"void fileRW(){ACE_FI ...