gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:35 25 4
gpt4 key购买 nike

我知道这方面已经有几个问题,但我想不通。之前解决的问题表明“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com