ios - 如何使用委托(delegate)将用户的输入保存在变量中?

标签 ios iphone swift tableview

我正在制作ios应用程序。请查看下面我的表格 View 图像。

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()方法。

// 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)
}

和addWordViewController.swift中的viewDidLoad()方法

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.
}

我不知道我必须使用哪个变量。 我的项目中还有其他 swift 文件,如果我错误上传了上面的代码,请告诉我!我会立即编辑我的问题。

enter image description here

主 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
        }
    }
}

最佳答案

我认为正确的方法是使用 WordTableViewController 中的 UITableViewDelegate 方法 didSelectRowAtindexPath (如果这是您的 TableView 的类)。

gestureRecognizer 看起来不像你想要在 tableViewController 中使用的东西,因为它有很好的内置委托(delegate)方法来注册用户按下。因此,请将 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]       
    }
}

这将获取您按下的单元格的英文单词,并将其保存到您随后在 UIReferenceLibraryViewController 中定义的属性“engWord”:

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

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

设置完成后,您可以在执行 segue 后随意使用它:=)

关于ios - 如何使用委托(delegate)将用户的输入保存在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46408473/

相关文章:

php - AFNetworking 解析 JSON url 图像错误

ios - `UITabBarController` 切换标签时去除黑色背景

iphone - UIButton AppStore 购买按钮动画

ios - 可以使用带有 itms-services 的自签名 SSL 证书

iphone - 如何在 Xcode 4 中使用 nib 创建自定义 UITableViewCell?

iphone - 如何使用 glRotatef() 和 glTranslatef() 在 3D 世界中增量移动

objective-c - 你如何比较 Swift 中的 NSFontSymbolicTraits 和 NSFontBoldTrait?

c - 链接 C 代码时出现重复符号错误

ios - 导入数据后CoreData内存未释放

ios - Sprite Kit iOS 中的慢动作