2017-09-25 34 views
0

我正在製作ios應用程序。請看下面我的表格視圖。如何使用委託將用戶的輸入保存在變量中?

enter image description here

如果您單擊添加按鈕(+),你可以在每個文本框添加ENG單詞及其在韓國(KOR)的意思。

enter image description here

填充所述文本字段和點擊保存按鈕(其位於右頂部「저장」)後,將單詞將被添加像下面的圖像。

word is added

例如,ENG不盡的含義(KOR)是 「끝없는」。

而且,我想使用UIReferenceLibraryViewController。 如果我點擊列表的單元格,我想顯示它的字典。

@IBAction func viewDictionary(_ sender: UITapGestureRecognizer) { 

    let engDictionaryWord = **engListWord** 
     let ViewController = UIReferenceLibraryViewController(term: engDictionaryWord) 
     self.present(ViewController, animated: true, completion: nil) 

} 

我想使用這種方法。 但是,我不知道如何保存我的ENG輸入engListWord

在pic2的swift文件(addWordViewController.swift)中,有這樣的prepare()方法。在addWordViewController.swift

override func viewDidLoad() { 
    super.viewDidLoad() 

    engTextField.delegate = self 
    korTextField.delegate = self 

    // Set up views if editing an existing Word. 
    if let word = word{ 
     engTextField.text = word.eng 
     korTextField.text = word.kor 
    } 

    // Do any additional setup after loading the view. 
} 

// This method lets you configure a view controller before it's presented. 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    super.prepare(for: segue, sender: sender) 

    // Configure the destination view controller only when the save button is pressed. 
    guard let button = sender as? UIBarButtonItem, button === saveButton else { 
     os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug) 
     return 
    } 

    let eng = engTextField.text ?? "" 
    let kor = korTextField.text ?? "" 

    // Set the meal to be passed to WordTableViewController after the unwind segue. 
    word = Word(eng:eng, kor:kor) 
} 

和viewDidLoad中()方法,我不知道我要使用的變量。 我的項目中還有其他swift文件,如果我錯誤地上傳了上面的代碼,請告訴我!我會立即編輯我的問題。

enter image description here

Main.Storyboard

enter image description here

如果我使用GestureRecognizer,我做了這個代碼,但我不知道這是正確的...

@IBAction func MyDictionary(_ sender: UITapGestureRecognizer) { 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     performSegue(withIdentifier: "viewDictionary", sender: indexPath) 
    } 

    func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // I just made this identifier up, but create one yourself in the storyboard 
     if segue.identifier == "viewDictionary" { 
      // Define your vc 
      let libController = segue.destination as! UIReferenceLibraryViewController 

      // Define indexPath 
      let indexPath = sender as! IndexPath 

      // Set value in destination vc 
      libController.engWord = words[indexPath.row].eng 
     } 
    } 
} 
+0

我真的不明白的問題。當你按下一個包含英文和韓文單詞的表格視圖單元格時,你想保存單詞? – matiastofteby

+0

@matiastofteby當我按下表格視圖單元格時,我想使用UIReferenceLibraryViewController顯示字典頁面。要使用UIReferenceLibraryViewController,ENG只是必需的事情。 因此,當我添加新單詞時,我想保存一個ENG文本,並將其與UIReferenceLibraryViewController代碼一起使用。但是,我無法保存英文文本。 要使用UIReferenceLibraryViewController,我必須將每個ENG文本保存在** engListWord ** –

+0

@matiastofteby簡而言之,我想保存每個表格視圖單元格的英文文本。 –

回答

0

我認爲要走的路是在WordTableViewController中使用UITableViewDelegate方法didSelectRowAtindexPath(如果該'與你的表格視圖類)。

一個gestureRecognizer似乎不像你想要在你的tableViewController中的東西,因爲它具有良好的委託方法註冊用戶按下內置版本。因此,用以下內容替換您的viewDictionary(_ :)功能:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "viewDictionary", sender: indexPath) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // I just made this identifier up, but create one yourself in the storyboard 
    if segue.identifier == "viewDictionary" { 
     // Define your vc 
     let libController = segue.destination as! UIReferenceLibraryViewController 

     // Define indexPath 
     let indexPath = sender as! IndexPath 

     // Set value in destination vc 
     libController.engWord = yourArrayOfEngWords[indexPath.row]  
    } 
} 

這將讓您按下電池的工程單詞並將其保存到一個屬性「engWord」你然後在UIReferenceLibraryViewController定義:

class UIReferenceLibraryViewController(){ 
    var engWord: String = "" 

    // Rest your class properties and functions here... 
} 

一旦它被設置爲你喜歡後SEGUE已經完成,你可以用它:=)

+0

我應該創建一個名爲UIReferenceLibraryViewController的新swift文件嗎? –

+0

我做了新的可可觸摸類名稱UIReferenceLibraryViewController。但是...有一個新的錯誤... –

+0

WordTableViewController.swift:80:42:無法指定'Word'類型的值來輸入'String' –

相關問題