Swift IOS8 XIB Issue viewcontroller.init () XIB

Source: Internet
Author: User

For oc Viewcontroller *VC = [[Viewcontroller alloc] init], the method defaults to loading a xib file with the same name as view. But in Swift viewcontroller.init () he does not load the Xib by default on IOS8, while in iOS 9 the xib is loaded by default.

The previous period of time project has been in the IOS9 + environment of the real machine debugging, today take IOS8 debugging, found that all the use of xib created Viewcontroller are not available. Even when it comes to an optional type. Helpless, build a test demo, find the problem. Finally solved the problem.

1, in Appdelegate, if you write this, because storyboard is not used, and xib is used, the content in Xib cannot be displayed in IOS8.

[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                &NBsp;      return true       }  

2, we need to use the nib method when we find the Xib and initialize it. This will show normal.

[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 =  "What is this   ah ... "       }     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.