Swift/IOS8 错误 : "fatal error: Can' t unwrap Optional. 无”

标签 swift ios8

我知道这方面已经有几个问题,但我想不通。之前解决的问题表明“profileViewController”为零,但我不知道为什么会这样。 UI 完全是程序化的,没有 IB。获取:

"fatal error: Can't unwrap 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
    }

screenshot

最佳答案

导航 Controller 对象self.navigationController nil吗?

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

为了防止崩溃,应该编写如下代码:

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

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

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

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

关于Swift/IOS8 错误 : "fatal error: Can' t unwrap Optional. 无”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621493/

相关文章:

swift - tableView.reloadData() 不工作,可能没有在主线程上调用

json - 使用 Swift Decoder 从 JSON 数组中提取属性

audio - 无法为iOS 8 Swift播放音频文件

ios - 带有自动布局的自定义 UITableViewCell 会导致警告

model-view-controller - 在 Swift 中传递数据

ios - 升级到 Swift 3 时使用未声明的类型

ios - swift ios 8 在表格 View 中更改部分的字体标题

ios - 将按钮限制在 View 中,使它们均匀分布

iOS 键盘扩展以 "per-process-limit"终止

ios - 当我们从 testflight 或 diawi iOS 下载构建时,推送通知不会出现