swift iOS8 XIB 问题 ViewController.init() xib

 

   对于OC 中 ViewController *vc = [[ViewController alloc] init],方法默认会加载一个同名的xib文件当View。但是在swift中  ViewController.init() 在 iOS8 上他不会默认加载xib,而在iOS 9 中却默认加载了 xib。

  前段时间工程一直在iOS9 + 环境的真机上调试,今天拿iOS8的机子调试,发现所有使用XIB创的 ViewController都不可用。甚至因为可选类型而到时奔溃。无奈之下,重新建一个测试demo,找问题。终于解决了这个问题。

1,在AppDelegate中,如果这样写,因为不使用storyboard,而使用XIB,在iOS8中,无法显示XIB中的内容。

[objc]  view plain  copy
  1. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  
  2.           
  3.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  4.         self.window?.makeKeyAndVisible()  
  5.           
  6.         let vc = myViewController()  
  7.         let navigation = UINavigationController.init(rootViewController: vc)  
  8.         self.window?.rootViewController = navigation  
  9.           
  10.         return true  
  11.     }  

2,我们需要在找到XIB,在初始化的时候,需要使用nib的方法。这样可以显示正常。

[objc]  view plain  copy
  1. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  
  2.           
  3.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  4.         self.window?.makeKeyAndVisible()  
  5.           
  6.         let vc: myViewController? = myViewController(nibName: "myViewController", bundle: nil)  
  7.         let navigation = UINavigationController.init(rootViewController: vc!)  
  8.         self.window?.rootViewController = navigation  
  9.           
  10.           
  11.         return true  
  12.     }  


[objc]  view plain  copy
  1. class myViewController: UIViewController {  
  2.   
  3.     @IBOutlet weak var titleLab: UILabel!  
  4.     override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {  
  5.         super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)  
  6.     }  
  7.       
  8.     required init?(coder aDecoder: NSCoder) {  
  9.         fatalError("init(coder:) has not been implemented")  
  10.     }  
  11.     override func viewDidLoad() {  
  12.         super.viewDidLoad()  
  13.         titleLab.text = "这是什么事 啊!!!!!"  
  14.     }  
  15.   
  16.     override func didReceiveMemoryWarning() {  
  17.         super.didReceiveMemoryWarning()  
  18.         // Dispose of any resources that can be recreated.  
  19.     }  

  解决方案
1. 通过加载 xib的方式来初始化ViewController (取代默认的 ViewController.init())

2.重写 init() 方法

//3.重写无参数初始化方法,自动调用xib文件

convenience override init() {

        var nibNameOrNil = String?("RootViewController")

        //考虑到xib文件可能不存在或被删,故加入判断

        if NSBundle.mainBundle().pathForResource(nibNameOrNil, ofType: "xib") == nil

        {

                nibNameOrNil = nil

        }

        self.init(nibName: nibNameOrNil, bundle: nil)

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值