2016-11-01 28 views
0

我正面臨一個奇怪的問題。我有一個註冊視圖控制器。從那裏我登錄完成後導航到第二個視圖控制器,其中有一個簡單的文本框。現在當我點擊文本框時,在執行textFieldDidBeginEditing之後,它給出了致命的錯誤。致命錯誤:在swift中編輯文本字段時展開可選值時意外發現nil

fatal error: unexpectedly found nil while unwrapping an Optional value 

接下來的事情我被困在斷點處

0xc7b108 <+68>: bl  0xcd0198     ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) ->()).(closure #2) 
0xc7b10c <+72>: trap 

事情我至今檢查。

  1. @IBOutlet已連接。
  2. textfield.delgate設置爲self。
  3. 文本框不爲零(在textFieldDidBeginEditing部分已記錄texfield變量)

現在最關心的事情是當我再次啓動應用程序並導航到同一視圖 - 控制(二視圖 - 控制),我可以編輯的文本字段。沒有陷阱或崩潰。 這件事情只有在我完成註冊後立即推送secondviewcontroller時纔會發生。

註冊後,我用這個代碼瀏覽第二視圖 - 控制

let instanceViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController 
self.navigationController?.pushViewController(instanceViewController, animated: true) 

而且我用下面的代碼,其登錄憑證保存在AppDelegate中didFinishLaunchingWithOptions導航第二視圖 - 控制

NB :只有當用戶在成功註冊後終止應用程序並嘗試再次啓動應用程序時,纔會執行此代碼。

let userDefaults = NSUserDefaults.standardUserDefaults() 
if let isUserLogin:Bool = userDefaults.boolForKey(ConstantsKey.UserDefault.isLogin){ 
    let objMainViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController 
    let navController = UINavigationController.init(rootViewController: objMainViewController) 

    self.window?.makeKeyAndVisible()    
    self.window?.rootViewController = navController 
} 

任何人都可以幫我嗎?

+0

'textFieldDidBeginEditing'裏面有什麼? – chengsam

+0

@chengsam:沒什麼。 – Poles

回答

0
let instanceViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as? SecondViewController 
0

對不起。我想我明白了。

這是因爲鍵盤keyboardWillShow通知。現在,keyboardWillShow通知函數在註冊視圖控制器中實現,並且我還使用同一個視圖控制器的另一個文本框變量。所以當keyboardWillShow爲secondviewcontroller執行時,它發現註冊頁面的textfield爲nil,這給了我一個致命的錯誤。

解決方案:我只需要從註冊視圖控制器視圖控制器中刪除觀察者deinit函數。

相關問題