网上似乎有很多教程,但我找不到将值从 TableView Controller 传递回第一个 View Controller 的教程。
这是我的设置... 第一个 ViewController 有一个文本字段,当用户按下它时,它会将他们带到第二个 TableViewController,在那里他们可以从列表中选择一个国家。一旦他们选择了它,我希望他们按下导航栏上的 DONE 按钮,然后我希望它回到第一个 ViewController 并在文本字段中显示他们选择的国家(准备好让他们按下按钮和应用程序继续等...)
据我所知,我需要在 DidSelectRowAtIndexPath 方法中执行此操作...?但我也经常看到关于放松转场的谈话?抱歉,我是 Swift 和 iOS 代码的新手,所以希望能明确回答什么是最好和最简单的方法。请在下面查看每个控制器的代码:
视图控制器
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textField: UITextField!
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.
}
}
表视图控制器
import UIKit
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tabeView: 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"]
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
}
}
提前致谢!