swift iOS8 XIB 問題 ViewController.init() xib

來源:互聯網
上載者:User

 

   對於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 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {                      self.window = UIWindow(frame: UIScreen.mainScreen().bounds)           self.window?.makeKeyAndVisible()                      let vc = myViewController()           let navigation = UINavigationController.init(rootViewController: vc)           self.window?.rootViewController = navigation                      return true       }  

2,我們需要在找到XIB,在初始化的時候,需要使用nib的方法。這樣可以顯示正常。

[objc]  view plain  copy func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {                      self.window = UIWindow(frame: UIScreen.mainScreen().bounds)           self.window?.makeKeyAndVisible()                      let vc: myViewController? = myViewController(nibName: "myViewController", bundle: nil)           let navigation = UINavigationController.init(rootViewController: vc!)           self.window?.rootViewController = navigation                                 return true       }  


[objc]  view plain  copy class myViewController: UIViewController {          @IBOutlet weak var titleLab: UILabel!       override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {           super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)       }              required init?(coder aDecoder: NSCoder) {           fatalError("init(coder:) has not been implemented")       }       override func viewDidLoad() {           super.viewDidLoad()           titleLab.text = "這是什麼事 啊。。。。。"       }     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.