Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are view controllers with nib files broken in ios 8 beta 5?

Tags:

xcode

ios

I created a test project in ios 8 beta 4 which as a main view controller and a second view controller created as a UIViewController subclass with a xib file.

I put a button on the main controller to present the second controller:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func testVCBtnTapped() {
    let vc = TestVC()
    presentViewController(vc, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

I run the app and pressing the button presents the second controller - all is well

Moving to xcode beta 5, I run the app and when I press the button the screen goes black.

Since I know they messed with the init code, I tried putting in overrides to see it that would fix it:

class TestVC: UIViewController {

override init() {
    super.init()
}
required init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

Same problem. Changing the required and overrides in to all possible combinations accepted by xcode has no effect.

If I use the storyboard to create another controller and segue to it all is well.

Any ideas?

EDIT - New info

  1. Tried nibName = nil in init - same problem
  2. Created the same app in objective c and it works fine

Apparently a swift beta 5 problem

like image 312
Jim T Avatar asked Aug 05 '14 22:08

Jim T


1 Answers

I've had this problem recently, and my solution is to override the nibName method in baseviewController. My solution is as follows:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

init() {
    super.init(nibName: nil, bundle: Bundle.main)
}

override var nibName: String? {
    get {
        if #available(iOS 9, *) {
            return super.nibName
        } else {
            let classString = String(describing: type(of: self))
            guard Bundle.main.path(forResource: classString, ofType: "nib") != nil else {
                return nil
            }
            return classString
        }
    }
}
like image 155
yinpan Avatar answered Nov 05 '22 03:11

yinpan