在Swift 中 ViewController 默认构造方法不关联同名的xib文件

在使用OC的时候,调用ViewController的默认构造函数,会自动关联到一个与ViewController名字相同的xib文件上,

这样无疑方便了开发者,减少了代码量。但不知道为什么,在swift中,新建ViewController实例,不在默认关联xib,

一定要开发者指定xib的名字,这对于有代码洁癖的人是不可接受的,因为通常情况下,

xib的名字一定是和ViewController的名字相同的。

// Objective C中的写法

TwoViewController *v = [[TwoViewController alloc] init]; //这样会把TwoViewController默认关联到TwoViewController.xib上

// 等同调用了方法

TwoViewController *v = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];

// Swift中的写法

let v = TwoViewController() //不会关联TwoViewController.xib文件

// 一定要指定xib文件

let v = TwoViewController(nibName: "TwoViewController", bundle: nil)

// 我们改怎样让TwoViewController()去找同名字的xib文件呢,
// 其实原来很简单,重写init方法就行了,
// 新建BaseViewController.swift 重写init方法,
// 其他的ViewController继续于BaseViewController。 override init() { // 获取当前Class的名字,在swift中class名字前会加入Module名字,所以这里以'.'划分,取'.'后面的字符串. super.init(nibName: (self.classForCoder.description() as NSString).componentsSeparatedByString(".")[1] as NSString, bundle: NSBundle.mainBundle()) } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }

  

Swift - IBOutlet返回nil(fatal error: unexpectedly found nil while unwrapping an Optional value)的更多相关文章

  1. Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value

    致命的错误: 对一个为空的optional值进行解包. 强制解包(forced unwrapping): 在optional后面加叹号. 比如  "value!" 错误代码: le ...

  2. PHP使用empty检查函数返回结果时报Fatal error: Can't use function return value in write context的问题

    PHP开发时,当你使用empty检查一个函数返回的结果时会报错:Fatal error: Can't use function return value in write context 例如: &l ...

  3. (转载)PHP使用empty检查函数返回结果时报Fatal error: Can't use function return value in write context的问题

    (转载)http://be-evil.org/post-153.html PHP开发时,当你使用empty检查一个函数返回的结果时会报错:Fatal error: Can't use function ...

  4. Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D

    Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D 从数据库 ...

  5. PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

    原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...

  6. 【nodejs】FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

    当使用大批量(>100)的SQL进行MySql数据库插值任务时,会发生以下错误: 总计将有371579条数据将被插入数据库 开始插入DB <--- Last few GCs ---> ...

  7. win7 X64 使用VS2008 ->编译报错LINK : fatal error LNK1000: Internal error during Incr的解决

    编译报错LINK : fatal error LNK1000: Internal error during Incr的解决 Win7 旗舰版 Microsoft Visual Studio 2008 ...

  8. php捕获Fatal error错误与异常处理

    php中的错误和异常是两个不同的概念. 错误:是因为脚本的问题,比如少写了分号,调用未定义的函数,除0,等一些编译语法错误. 异常:是因为业务逻辑和流程,不符合预期情况,比如验证请求参数,不通过就用 ...

  9. Zen Cart 138 在PHP5.3环境下出现的Fatal error: Cannot redeclare date_diff()

    Zen Cart 138 在PHP5.3环境下出现的Fatal error: Cannot redeclare date_diff() in includes/functions/functions_ ...

随机推荐

  1. mysql 整形的长度问题

    tinyint  有符号的范围是-128至127: 无符号的范围是0到255,2的8 次方-1: 存储大小为 1 字节. smallint  有符号的范围是-32768至32767: 无符号的范围是0 ...

  2. PDO封装函数

    header("Content-type: text/html; charset=utf-8"); /** * 初始化 pdo 对象实例 * @param bool $newins ...

  3. php 学习路线 赵兴壮2014年4月28 日 加油

    第一阶段 第一讲,WEB基础     1.1 网站基本知识: 1.2 网络协议介绍: 1.3 B/S与C/S结构的区别: 1.4 WEB编程.网站开发技术介绍.      第二讲,网页设计     2 ...

  4. idHTTP最简洁的修改和取得Cookie例子

    procedure TForm1.Button1Click(Sender: TObject); var HTTP: TidHTTP; html, s: string; i: integer; begi ...

  5. 使用jsoup解析html页面内容案例

    public String getFaGuiKuTitles(String type, int page) { String href = "http://info.qd-n-tax.gov ...

  6. hdu 4544 湫湫系列故事——消灭兔子

    http://acm.hdu.edu.cn/showproblem.php?pid=4544 优先队列+贪心. #include <cstdio> #include <queue&g ...

  7. Android系统的进程分类

    1.前台进程:即当前正在前台运行的进程,说明用户当前正在与通过该进程与系统进行交互,所以该进程为最重要的进程,除非系统的内容已经到不堪重负的情况,否则系统是不会将改进程终止的.2.可见进程:一般还是显 ...

  8. Collections.shuffle源码阅读

    java.util.Collections /** * Randomly permutes the specified list using a default source of * randomn ...

  9. mybatis mapper namespace

    http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete org.apache.ibatis.excep ...

  10. 俄罗斯方块:Python实现

    网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的 主程序: #coding:utf8 #! /usr/bin/env python # ...