【问题标题】:Swift/IOS8 error: "fatal error: Can't unwrap Optional.None"Swift/IOS8 错误:“致命错误:无法打开 Optional.None”
【发布时间】:2014-07-07 23:26:07
【问题描述】:

我知道这方面已经存在一些问题,但我无法弄清楚。先前解决的问题表明“profileViewController”为零,但我不知道为什么会这样。 UI 完全是程序化的,没有 IB。获取:

“致命错误:无法打开 Optional.None”

关于 pushViewController() 的代码如下:

class FavoritesViewController: UIViewController {

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
        self.title = "Favorites"
        self.tabBarItem.image = UIImage(named: "MikeIcon")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.redColor()

        let profileButton = UIButton.buttonWithType(.System) as UIButton
        profileButton.frame = CGRectMake(60, 300, 200, 44)
        profileButton.setTitle("View Profile", forState: UIControlState.Normal)
        profileButton.addTarget(self, action: "showProfile:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(profileButton)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func showProfile(sender: UIButton) {
        let profileViewController = ProfileViewController(nibName: nil, bundle: nil)
        self.navigationController.pushViewController(profileViewController, animated: true)

    }

这是 AppDelegate.swift 的相关部分:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // Override point for customization after application launch.

        let feedViewController = FeedViewController(nibName: nil, bundle: nil)
        let favoritesViewController = FavoritesViewController(nibName: nil, bundle: nil)
        let profileViewController = ProfileViewController(nibName: nil, bundle: nil)

        let tabBarController = UITabBarController()
        self.window!.rootViewController = tabBarController

        tabBarController.viewControllers = [feedViewController, favoritesViewController, profileViewController]        


        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        return true
    }

【问题讨论】:

  • 尝试使用不同的初始化程序?为什么不只是ProfileViewController()

标签: swift ios8


【解决方案1】:

导航控制器对象是self.navigationControllernil吗?

navigationController 属性上的变量类型是未包装的可选项。如果您的 View Controller 不在 UINavigationController 内,那就是问题所在。

为了防止崩溃代码应该写成如下几行:

if (self.navigationController)
{
    self.navigationController.pushViewController(profileViewController, animated: true) 
}

或者,您可以将代码编写为:

self.navigationController?.pushViewController(profileViewController, animated: true)

如果self.navigationController 的计算结果为nil? 运算符将阻止执行任何进一步的代码。

【讨论】:

  • 是的,NavigationController 是 nil,所以上面的测试确实捕获了它并防止了异常。我得回去看看为什么会这样。我正在尝试学习 Codeschool 上的“试用 IOS”课程,并即时将他们的示例转换为 Swift,因此我并不总是能看到他们的整个代码库。
  • 我的错,我错过了 Codeschool 课程从 UITabViewController 更改为 UINavigationController 的地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
相关资源
最近更新 更多