Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在iOS 8上以模式呈现NIB会崩溃,但在iOS 9上不会+;_Swift_Xcode_Crash_Nib - Fatal编程技术网

Swift 在iOS 8上以模式呈现NIB会崩溃,但在iOS 9上不会+;

Swift 在iOS 8上以模式呈现NIB会崩溃,但在iOS 9上不会+;,swift,xcode,crash,nib,Swift,Xcode,Crash,Nib,我创建了一个名为SomeViewController的NIB,所有对应的代码都正确,所有视图都绑定正确,但代码self.presentViewController(SomeViewController(),动画:true,完成:nil)会导致崩溃: 致命错误:在展开可选值时意外发现nil 问题是什么?要解决此问题,我们需要通过执行此操作进行版本检查 if #available(iOS 8, *) { self.presentViewController(SomeViewC

我创建了一个名为
SomeViewController
的NIB,所有对应的代码都正确,所有视图都绑定正确,但代码
self.presentViewController(SomeViewController(),动画:true,完成:nil)
会导致崩溃:

致命错误:在展开可选值时意外发现nil


问题是什么?

要解决此问题,我们需要通过执行此操作进行版本检查

    if #available(iOS 8, *) {
        self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
    } else {
        self.presentViewController(SomeViewController(), animated: true, completion: nil)
    }
class SomeViewController: UIViewController {
    init() {
        super.init(nibName: "SomeViewController'sNibNameHere", bundle: nil)
    }
}

// on some other part of your code
self.presentViewController(SomeViewController(), animated: true, completion: nil)
或者只是

self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
由于某些原因,iOS 8在初始化时没有自动将nibName包含在其相应的类中

更新:

也可以通过这样做来修复

    if #available(iOS 8, *) {
        self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
    } else {
        self.presentViewController(SomeViewController(), animated: true, completion: nil)
    }
class SomeViewController: UIViewController {
    init() {
        super.init(nibName: "SomeViewController'sNibNameHere", bundle: nil)
    }
}

// on some other part of your code
self.presentViewController(SomeViewController(), animated: true, completion: nil)

要解决这个问题,我们需要通过执行以下操作进行版本检查

    if #available(iOS 8, *) {
        self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
    } else {
        self.presentViewController(SomeViewController(), animated: true, completion: nil)
    }
class SomeViewController: UIViewController {
    init() {
        super.init(nibName: "SomeViewController'sNibNameHere", bundle: nil)
    }
}

// on some other part of your code
self.presentViewController(SomeViewController(), animated: true, completion: nil)
或者只是

self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
由于某些原因,iOS 8在初始化时没有自动将nibName包含在其相应的类中

更新:

也可以通过这样做来修复

    if #available(iOS 8, *) {
        self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
    } else {
        self.presentViewController(SomeViewController(), animated: true, completion: nil)
    }
class SomeViewController: UIViewController {
    init() {
        super.init(nibName: "SomeViewController'sNibNameHere", bundle: nil)
    }
}

// on some other part of your code
self.presentViewController(SomeViewController(), animated: true, completion: nil)

崩溃日志是什么?@AhmadF
致命错误:在打开可选值时意外发现nil
我已经用下面的答案修复了它。尽管崩溃日志是什么,但为什么会发生这种情况还是很奇怪的?@AhmadF
致命错误:在打开可选值时意外地发现了nil
我已经用下面的答案修复了它。虽然你应该标记你的答案,但为什么会发生这种情况却很奇怪,因为现在这似乎和任何答案一样有效:)。我不得不等2天来标记我自己的答案。哈哈,你应该标记你的答案,因为现在这似乎和任何答案一样有效:)。我不得不等2天来标记我自己的答案哈哈