2017-07-05 86 views
-1

我有一個Viewcontroller,它有一個View外部它(在這3個符號(viewcontroller,first responder,exit)之間),當我點擊一個按鈕時,它顯示了我的View,然後我爲它創建了一個類文件,並且此視圖中的文本字段數據必須返回到ViewController。 我該怎麼做?我只通過segues知道方法。 tksViewController中UIview之間傳遞數據

+0

[UITextFieldDelegate](https://developer.apple.com/documentation/uikit/uitextfielddelegate) – Frankie

+0

對不起,我新在迅速,我需要一個例子。我該怎麼做:重寫func prepare(for segue:UIStoryboardSegue,sender:Any?){ if segue.identifier == segueAddQuoteViewController { if let destinationViewController = segue.destination as? AddQuoteViewController {// 配置視圖控制器 destinationViewController.managedObjectContext = persistentContainer.viewContext }} } 私人讓segueAddQuoteViewController = 「SegueAddQuoteViewController」 ...........我所需要的? –

+1

@tiago .. - 不要在評論中張貼代碼。請[編輯](https://stackoverflow.com/posts/44935196/edit)您的問題與所有相關的細節。 – DonMag

回答

0

如果我正確理解你的問題,你試圖引用你的ViewController中的視圖對象。爲此,您不需要單獨的視圖類。在ViewController中有兩個引用視圖對象的選項:

  1. 在ViewController中加載視圖後,在代碼中創建對象;
  2. 在UI編輯器中將對象連接到@IBOulets。

第二個選項更容易。主要是如果你從Swift開始。你可以學習在example中這樣做。或者檢查這個計算器answer

0
  1. 告訴你的視圖控制器它符合UITextFieldDelegate協議。

    class MyViewController : UIViewController <UITextFieldDelegate> 
    
  2. 確保您的UITextField委託屬性被連接到視圖控制器。您可以通過IB或代碼來完成此操作。

    @IBOutlet weak var myTextField: UITextField! 
    
    //... later on... 
    
    myTextField.delegate = self 
    
  3. 從您的視圖控制器中的引用實現任何UITextFieldDelegate方法。例如:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
        print("text value is: \(textField.text)") 
    } 
    
相關問題