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.
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.
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_ ...
随机推荐
- elasticsearch-5.0.0初见
基础概念 Elasticsearch有几个核心概念.从一开始理解这些概念会对整个学习过程有莫大的帮助. 接近实时(NRT) Elasticsearch是一个接近实时的搜索平台.这意味着,从索引一个文档 ...
- Flask源码解读(一)
Flask是一个使用 Python 编写的轻量级 Web 应用框架.Flask 本身只是 Werkezug 和 Jinja2 的之间的桥梁,前者实现一个合适的 WSGI 应用,后者处理模板. 当然, ...
- C语言实现约瑟夫环讨论
[问题描述] 约瑟夫(Joseph)问题的一种描述是:编号为1,2,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数).一开始任选一个正整数作为报数上限值m,从第一个人开始按顺时针 ...
- 转:js不同类型对象的比较规则
Type(x) Type(y) Result type(x)==type(y) x===y otherwise... false null undefined true undefined null ...
- 手机SIM卡介绍 三类不同标准的SIM卡
SIM卡的全称是Subscriber Identity Module,翻译过来也叫客户识别模块,也叫做智能卡.用户身份识别卡.这块小小的芯片可以存储用户的号码.信息,以及一定数量的联系人数据,配合我们 ...
- android 大小写转换
private void toUpperCase(byte[] data, int start, int len) { int end = start + len; int dist = 'A' - ...
- What is a good EPUB reader on Linux
Last updated on August 20, 2014 Authored by Adrien Brochard 12 Comments If the habit on reading book ...
- $.getJSON(url,function success(){})回调函数不起作用
有个问题好久没有解决,就是: $.getJSON(url,function success(){}) 其中的回调函数,总也不执行. 以前也做过,但那都是CTRL+C,CTRL+V,也没有细想. 目标就 ...
- ADO.NET基础笔记
ADO.NET 程序要和数据库交互要通过ADO.NET进行,通过ADO.Net就能在程序中执行SQL了. ADO.Net中提供了对各种不同的数据库的统一操作接口. 连接字符串: 程序通过连接字符串指定 ...
- [译]Stairway to Integration Services Level 5 - 增量删除数据
在 dbo.Contact中添加一行记录 Use AdventureWorks go Insert Into dbo.Contact (FirstName, MiddleName, LastName, ...