2014-07-07 24 views
4

我知道這已經有幾個問題,但我無法弄清楚。以前解決的問題會提示'profileViewController'爲零,但我不知道爲什麼會這樣。 UI是完全程序化的,沒有IB。獲得:在下面的代碼Swift/IOS8錯誤:「致命錯誤:不能展開Optional.None」

"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

+0

嘗試使用不同的初始值設定項?爲什麼不只是'ProfileViewController()' – Jack

回答

2

的是導航控制器對象self.navigationControllernil

navigationController屬性上的變量類型是unwrapped可選項。如果您的視圖控制器不在UINavigationController之內,那就是問題所在。

,防止死機代碼大致如下應該寫成:

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

另外,您可以編寫代碼:

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

?運營商將阻止任何進一步的代碼被執行,如果self.navigationController評估爲nil

+0

是的,NavigationController是零,所以上面的測試確實抓住它並防止異常。我必須回去看看這是爲什麼。我試圖遵循Codeschool上的「試用IOS」課程,並將其示例轉換爲Swift,因此我並不總是看到他們的整個代碼庫。 – Sap

+0

我的錯,我錯過了Codechool課程從UITabViewController更改爲UINavigationController的地方。 – Sap