我正在制作ios应用程序。请查看下面我的表格 View 图像。
如果单击添加 (+) 按钮,您可以在每个文本字段中添加 ENG 单词及其韩语 (KOR) 含义。
填写文本字段并单击“保存”按钮(位于右上角“저장”)后,将添加该单词,如下图所示。
例如,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 文件,如果我错误上传了上面的代码,请告诉我!我会立即编辑我的问题。
主 Storyboard
如果我使用 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/