swift iOS8 XIB 无法显示问题

今天遇到一个很无语的问题,工程一直在iOS9环境的真机上调试,今天拿iOS8的机子调试,发现所有使用XIB创建的

ViewController都不可用,我屮艸芔茻,这是怎么回事,无语啊。

无奈之下,重新建一个测试demo,找问题。终于解决了这个问题,但是却不明白为什么在iOS9中可以,在iOS8中不可以!!!!!!

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 = "这是什么事 啊!!!!!"

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

你可能感兴趣的:(swift iOS8 XIB 无法显示问题)