2017-05-20 61 views
0

我有一個提交的UIButton是針對這個功能,我有一個textField,我想傳遞到下一個視圖控制器。因此,用戶可以輸入textField,無論輸入什麼內容,一旦用戶點擊提交按鈕,文本字段內的字符串將被傳遞到下一個視圖控制器的UILabel。如何通過導航推送數據視圖控制器

func submitTapped() { 
     let secondViewController = SecondViewController(nibName: nil, bundle: nil) 
     navigationController?.pushViewController(secondViewController, animated: true) 

     print("Button tapped") 
    } 
} 

如何通過第二個控制器推送該字符串?另外,我嘗試過使用prepareForSegue,但我認爲這與segues無關,因爲我們不是通過segue轉換屏幕,我們是通過導航控制器轉換的,對嗎?

謝謝!

+0

哦,我解決了這個問題。我所要做的就是從secondViewController訪問標籤。謝謝! –

+0

[在視圖控制器之間傳遞數據]的可能重複(http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) –

回答

1

在SecondViewController中,創建一個變量並在創建對象時分配值。檢查下面的例子:

class SecondViewController { 
var dataString: String? 
} 

通行證數據象下面這樣:

func submitTapped() { 
     let secondViewController = SecondViewController(nibName: nil, bundle: nil) 
     secondViewController.dataString = textField.text! 
     navigationController?.pushViewController(secondViewController, animated: true) 
     print("Button tapped") 
    } 
} 
+0

謝謝!但是,這不會在兩個班級之間產生緊密的聯繫嗎?有另一種方法可以實現這一目標嗎? –

0

在YourSecondViewController,聲明一個變量。使用下面的代碼

class YourSecondViewController { 
    var strData: String? 
    } 

傳遞數據:

func submitTapped() { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let objSecond = storyboard.instantiateViewController(withIdentifier: "YourSecondViewControllerID") 
    objSecond.strData = yourTextField.text! 
    self.navigationController?.pushViewController(objSecond, animated: true) 
} 
相關問題