Uitableview 选择TableView单元格后,将数据传回Swift中的上一个视图控制器
在线上似乎有很多这方面的教程,但我找不到一个可以将值从TableView控制器传递回第一个View控制器的教程 这是我的设置。。。第一个ViewController有一个文本字段,当用户按下该字段时,它会将他们带到第二个TableViewController,在那里他们可以从列表中选择一个国家。一旦他们选择了它,我希望他们按下导航栏上的“完成”按钮,然后我希望它返回到第一个ViewController,并在文本字段中显示他们选择的国家,以便他们按下按钮,应用程序继续等 根据我的计算,我需要在DidSelectRowAtIndexPath方法中执行此操作。。。?但我也一直在看关于放松环节的谈话?很抱歉,我对Swift和iOS代码不熟悉,因此希望您能给出一个清晰的答案,告诉我什么是最好、最简单的方法。请参阅下面每个控制器的代码: 视图控制器 TableViewControllerUitableview 选择TableView单元格后,将数据传回Swift中的上一个视图控制器,uitableview,swift,segue,pass-data,unwind-segue,Uitableview,Swift,Segue,Pass Data,Unwind Segue,在线上似乎有很多这方面的教程,但我找不到一个可以将值从TableView控制器传递回第一个View控制器的教程 这是我的设置。。。第一个ViewController有一个文本字段,当用户按下该字段时,它会将他们带到第二个TableViewController,在那里他们可以从列表中选择一个国家。一旦他们选择了它,我希望他们按下导航栏上的“完成”按钮,然后我希望它返回到第一个ViewController,并在文本字段中显示他们选择的国家,以便他们按下按钮,应用程序继续等 根据我的计算,我需要在Di
提前谢谢 在原始视图控制器中添加要处理回拨的iAction 在IB Control中,从单元格拖动到视图顶部的退出图标。在发布时,您将看到一个弹出菜单,其中包含可用iAction方法的名称。选择您的IBAction方法 我不记得如果向iAction添加发送方变量,是否可以从中检索信息。在第二个视图控制器中添加一个弱变量previousVC:UIViewController,其中包含上一个视图控制器表视图控制器。在转到第二个视图控制器之前,请在第一个视图控制器中添加prepareForSegue方法,并使用let destinationVC=segue.destinationViewController然后使用destinationVC.previousVC=self访问第二个视图控制器。现在,您可以访问以前的控制器,通过向第一个视图控制器添加属性,可以在其中传递所需的任何数据 代码: 第二视图控制器 导入UIKit 类TableViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
@IBOutlet var tableView: UITableView!
var countryPicker = ["Colombia", "England", "France", "Germany", "Brazil", "Austria", "Spain", "Australia", "USA", "Berlin", "Thailand", "Northern Ireland", "New Zealand", "Hawaii", "Switzerland", "Sweeden", "Finland", "Turkey", "Russia", "Netherlands", "Japan"]
var delegate: CountrySelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryPicker.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
cell.textLabel!.text = countryPicker[indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let country = countryPicker[indexPath.row]
self.delegate.countryDidSelect(country: country)
self.navigationController?.popViewController(animated: true)
}
@IBOutlet var textField: UITextField!
@IBOutlet var lblCountry: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
performSegueWithIdentifier("countryTableView", sender: self)
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func gotoSecondVC(_ sender: Any) {
let secondVc = self.storyboard?.instantiateViewController(identifier: "TableViewController") as! TableViewController
secondVc.delegate = self
navigationController?.pushViewController(true, animated: true)
}
func countryDidSelect(country: String) {
lblCountry.text = country
}
}
协议国家选择代表{
func countryDidSelectcountry:字符串
}第一视图控制器
导入UIKit
类ViewController:UIViewController、UIExtFieldDelegate{
@IBOutlet var tableView: UITableView!
var countryPicker = ["Colombia", "England", "France", "Germany", "Brazil", "Austria", "Spain", "Australia", "USA", "Berlin", "Thailand", "Northern Ireland", "New Zealand", "Hawaii", "Switzerland", "Sweeden", "Finland", "Turkey", "Russia", "Netherlands", "Japan"]
var delegate: CountrySelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryPicker.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
cell.textLabel!.text = countryPicker[indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let country = countryPicker[indexPath.row]
self.delegate.countryDidSelect(country: country)
self.navigationController?.popViewController(animated: true)
}
@IBOutlet var textField: UITextField!
@IBOutlet var lblCountry: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
performSegueWithIdentifier("countryTableView", sender: self)
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func gotoSecondVC(_ sender: Any) {
let secondVc = self.storyboard?.instantiateViewController(identifier: "TableViewController") as! TableViewController
secondVc.delegate = self
navigationController?.pushViewController(true, animated: true)
}
func countryDidSelect(country: String) {
lblCountry.text = country
}
}
扩展视图控制器:CountrySelectionDelegate{
@IBOutlet var tableView: UITableView!
var countryPicker = ["Colombia", "England", "France", "Germany", "Brazil", "Austria", "Spain", "Australia", "USA", "Berlin", "Thailand", "Northern Ireland", "New Zealand", "Hawaii", "Switzerland", "Sweeden", "Finland", "Turkey", "Russia", "Netherlands", "Japan"]
var delegate: CountrySelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryPicker.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
cell.textLabel!.text = countryPicker[indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let country = countryPicker[indexPath.row]
self.delegate.countryDidSelect(country: country)
self.navigationController?.popViewController(animated: true)
}
@IBOutlet var textField: UITextField!
@IBOutlet var lblCountry: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
performSegueWithIdentifier("countryTableView", sender: self)
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func gotoSecondVC(_ sender: Any) {
let secondVc = self.storyboard?.instantiateViewController(identifier: "TableViewController") as! TableViewController
secondVc.delegate = self
navigationController?.pushViewController(true, animated: true)
}
func countryDidSelect(country: String) {
lblCountry.text = country
}
}对不起,我不明白你的意思。你能把它添加到我的代码中,让我看看你在说什么吗?谢谢谢谢你的回答,我对代码还不熟悉,所以发现下面有点让人困惑:/你介意在上面的代码上给我看看我在哪里写这些代码,这样我就可以更好地理解它了。我感谢你的帮助!通常,会使用协议方法将数据传递回上一个VC。还可以使用其他东西,比如NSNotification