2016-03-02 82 views
2

當我嘗試推送另一個控制器時,我遇到了UINavigationController問題。Swift - UINavigation Controller變爲零

的AppDelegate

let testViewController:ProximityQuestionsIntroductionViewController = ProximityQuestionsIntroductionViewController(); 
    self.navController = UINavigationController(rootViewController: testViewController); 

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds); 
    self.window!.rootViewController = navController; 
    self.window!.backgroundColor = UIColor.whiteColor(); 
    self.window!.makeKeyAndVisible(); 

    return true 

ProximityQuestionsIntroductionViewController

class ProximityQuestionsIntroductionViewController:UIViewController { 

// Controls 
var proximityIntroductionView:ProximityQuestionsIntroductionView!; 

// Load components when the view is loaded 
override func viewDidLoad() { 
    super.viewDidLoad(); 

    print("nav in viewDidLoad : \(self.navigationController)"); 
    //Hide Nav bar 
    self.navigationController?.navigationBarHidden = true; 
    // Set view 
    self.proximityIntroductionView = ProximityQuestionsIntroductionView(frame: self.view.frame); 
    self.view = self.proximityIntroductionView; 
} 

func start(){ 
    print(self); 
    print("start in controller"); 
    let nextController = ProximityQuestionsProgressViewController(); 
    print("nav in start : \(self.navigationController)"); 
    self.navigationController?.pushViewController(nextController, animated: true); 

} 

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

我打電話從一個按鈕被按下時,我ProximityQuestionsIntroductionViewstart。 在日誌中,我看到「導航在開始:無」,並且控制器不會更改。

欲瞭解更多信息:

  • 自我是真正的ProximityQuestionsIntroductionViewController

  • 導航欄真隱

所以導航控制器是viewDidLoad確定,但不是在其他功能。

有人能告訴我爲什麼嗎?

謝謝!

編輯:AppDelegate中

 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 

    let initialViewController = storyboard.instantiateViewControllerWithIdentifier("ProximityIntroduction"); 
    self.navController = storyboard.instantiateViewControllerWithIdentifier("NavigationController") as! UINavigationController; 
    self.navController?.setViewControllers([initialViewController], animated: false) 
    self.window?.rootViewController = navController; 
    self.window?.makeKeyAndVisible(); 

我添加導航控制器到故事板並更改ID。但我仍然有對UINavigationController一個nil值的函數start

回答

0

問題來自視圖。

這樣做:

// Set view 
    self.proximityIntroductionView = ProximityQuestionsIntroductionView(frame: self.view.frame); 
    self.view = self.proximityIntroductionView; 

導致該問題。我不知道爲什麼,但將所有UIView放入控制器直接解決了問題。

0
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 

    let initialViewController = storyboard.instantiateViewControllerWithIdentifier("ProximityQuestionsIntroductionViewController") 
    navigationController = UINavigationController.init(rootViewController:initialViewController) 
    navigationController?.setViewControllers([initialViewController], animated: false) 
    self.window?.rootViewController = navigationController 
    self.window?.makeKeyAndVisible() 

認沽故事板ID,此ProximityQuestionsIntroductionViewController

+0

我試圖做所有我的項目沒有故事板,所以我有一個:'NSInvalidArgumentException',原因:'故事板()不包含視圖控制器與標識符'ProximityQuestionsIntroductionViewController'' –

+0

那麼你使用xib進行設計? –

+0

我以編程方式完成這一切。這是一個由其他人開始的項目,我不能讀故事板它是一團糟。 –

0

你必須標記窗口和navController變量可選:

var window : UIWindow? 
var navController : UINavigationController? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    let testViewController:ProximityQuestionsIntroductionViewController = ProximityQuestionsIntroductionViewController(); 

    self.navController = UINavigationController(rootViewController: testViewController); 

    if let window = window { 

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds); 
    self.window!.rootViewController = navController; 
    self.window!.backgroundColor = UIColor.whiteColor(); 
    self.window!.makeKeyAndVisible(); 
    } 
    return true 
} 

如果您是通過Xcode模板創建項目的: 從Info.plist中刪除鍵「Main storyboard file base name」的鍵值對。 刪除故事板文件Main.storyboard。

+0

這就是我第一次發佈問題時的代碼。它不起作用,self.navigationController在第一個控制器的函數啓動中暫停。 –