1

网上似乎有很多教程,但我找不到将值从 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
}

}

提前致谢!

4

4 回答 4

0

weak var previousVC: UIViewController在您的第二个视图控制器(您的表视图控制器)中添加一个包含您以前的视图控制器。在进入你的第二个视图控制器之前,prepareForSegue在你的第一个视图控制器中添加一个方法,然后使用let destinationVC = segue.destinationViewControllerthen访问你的第二个视图控制器destinationVC.previousVC = self。你现在可以访问你以前的控制器,你可以通过向你的第一个视图控制器添加一个属性来传递你想要的任何数据。

代码:

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var textField: UITextField!

var myObject: Object? // Some object you will pass
override func viewDidLoad() {
    super.viewDidLoad()

}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

performSegueWithIdentifier("countryTableView", sender: self)

return false

}
...

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "countryTableView" {
        let tableVC = segue.destinationViewController as! TableViewController
        // Pass the object
        tableVC.passedObject = myObject
    }
}
}


class TableViewController.... {
var passedObject: Object // The object to be passed
...
}
于 2015-06-04T21:49:17.503 回答
0

第一个视图控制器

导入 UIKit

类视图控制器:UIViewController,UITextFieldDelegate {

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

}

扩展 ViewController: CountrySelectionDelegate {

func countryDidSelect(country: String) {
    lblCountry.text = country
}

}

于 2021-03-18T16:55:28.480 回答
-1

在原始视图控制器中添加一个您想要处理回调的 IBAction。

在 IB Control 中,从单元格拖动到视图顶部的退出图标。在发布时,您将看到一个带有可用 IBAction 方法名称的弹出菜单。选择您的 IBAction 方法。

我不记得是否将发送者变量添加到 IBAction,您可以从中检索信息。

于 2015-06-04T21:43:33.833 回答
-1

第二个视图控制器

导入 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)
}

}

协议 CountrySelectionDelegate { func countryDidSelect(country: String) }

于 2021-03-18T16:45:57.837 回答