我有两个ViewControllers,一个包含UITextView,另一个包含UITableView。我希望我的应用程序将包含UITableView的SecondViewController中所选行的数据传递给第一个ViewController中的UITextView,具体取决于用户选择的行。我在firstViewController中使用下面的代码(只是为了给你一些历史记录,我在firstViewController中有一个UITextView,用户可以选择输入自定义值或执行longpressgesture然后弹出窗口显示包含第二个ViewController中的UITableView。我想要实现的是当从包含UItableView的popoverView中选择一行时,popoverView将关闭,并且表中突出显示的值将显示在firstViewController的UITextView中:
class ViewController: UIViewController, UITextViewDelegate, UIPopoverPresentationControllerDelegate {
@IBOutlet weak var indicativeDesignWorkingLifeTextView: UITextView!
var textInsideIndicativeDesignWorkingLifeTextView: String? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
indicativeDesignWorkingLifeTextView.text = textInsideIndicativeDesignWorkingLifeTextView
indicativeDesignWorkingLifeTextView.attributedText = placeholderTextInIndicativeDesignWorkingLifeTextView
}
}
以及secondViewController中的以下代码:
@objc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
func prepare(for segue: UIStoryboardSegue, sender: UITableViewCell?) {
let toFirstViewController = segue.destination as! ViewController
// Pass the selected object to the new view controller.
if let indexPath = self.indicativeDesignWorkingLifeTable.indexPathForSelectedRow {
let selectedRow = years[indexPath.row]
toFirstViewController.textInsideIndicativeDesignWorkingLifeTextView = selectedRow
}
}
}
但是,当我运行模拟器并从表中选择一行时,firstConController中的UITextView内部没有任何反应?所有发生的事情都是显示firstViewController。非常感谢任何帮助。
谢谢, 沙迪。
答案 0 :(得分:0)
将您的代码更新为:
@objc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "<set identifier String>", sender: indexPath)
}
func prepare(for segue: UIStoryboardSegue, sender:Any?) {
let toFirstViewController = segue.destination as! ViewController
// Pass the selected object to the new view controller.
if let indexPath = sender as? IndexPath {
print("indexPath - \(indexPath)")
let selectedRow = years[indexPath.row]
print("selectedRow - \(selectedRow)")
toFirstViewController.textInsideIndicativeDesignWorkingLifeTextView = selectedRow
}
}