2016-10-04 36 views
2
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let newuser = users[indexPath.row] 
    let usernname = newuser.name! as String 
    print(usernname) 
    print(keys[indexPath.row]) 
    let destinationViewController = ChatViewController() 
    destinationViewController.toUid = keys[indexPath.row] 
    destinationViewController.topBar.title = usernname 
    performSegue(withIdentifier: "toChatView", sender: self) 
} 

鍵[indexPath.row]工作正常,但用戶名有錯誤。它說Segue可選值無

fatal error: unexpectedly found nil while unwrapping an Optional value 

但是,當我打印我username變量,它打印OK

+0

哪裏就是你的執行SEGUE方法? –

+0

嘗試使用if let usernname = newuser.name!作爲字符串{做代碼如果條件} –

+0

@ Mr.UB那麼,它在我的代碼 –

回答

0

你不應該設置這樣的標題頂欄,你正在做它在你的ChatViewControllertitleUsername,聲明一個新變量並通過username它和裏面ChatViewControllerself.title = titleUsername來設置標題。

+0

正確,但'讓destinati ...'代碼是沒用的嗎? –

+1

@ Mr.UB,該代碼應該設置在'func prepare(for segue:UIStoryboardSegue ...'代碼 –

+0

謝謝,它提供了我的錯誤的答案,但topBar的標題仍然不會改變 –

0

你需要檢查,如果用戶名是零或不

let usernname = newuser.name! as String 

let destinationViewController = ChatViewController() 
destinationViewController.toUid = keys[indexPath.row] 

if username != nil { // check if this is nil or not 
print(usernname) 
print(keys[indexPath.row]) 
destinationViewController.topBar.title = usernname 
performSegue(withIdentifier: "toChatView", sender: self) 
}