【问题标题】:After selecting a TableView Cell, pass the data back to the previous View Controller in Swift选择一个 TableView Cell 后,将数据传回 Swift 中的前一个 View Controller
【发布时间】:2015-08-19 16:34:07
【问题描述】:

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

}

TableViewController

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
}

}

提前致谢!

【问题讨论】:

  • 通常会使用协议方法将数据传回前一个 VC。可以使用其他东西,例如 NSNotification

标签: uitableview swift segue pass-data unwind-segue


【解决方案1】:

在您的第二个视图控制器(您的表视图控制器)中添加一个包含您以前的视图控制器的weak var previousVC: UIViewController。在进入你的第二个视图控制器之前,在你的第一个视图控制器中添加一个prepareForSegue 方法,然后使用let destinationVC = segue.destinationViewController 然后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
...
}

【讨论】:

  • 感谢您的回答,我是代码新手,所以发现有点混乱:/ 你介意在我上面的代码中显示我将在哪里写这些位以便我能理解它更多的。感谢您的帮助!
【解决方案2】:

第一个视图控制器

导入 UIKit

类 ViewController: 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)
}

}

扩展视图控制器:CountrySelectionDelegate {

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

}

【讨论】:

    【解决方案3】:

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

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

    我不记得如果你向 IBAction 添加一个发送者变量,你可以从中检索信息。

    【讨论】:

    • 对不起,我不明白你的意思。您能否将其添加到我的代码中以显示您在说什么?谢谢
    【解决方案4】:

    第二个视图控制器

    导入 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(国家:字符串) }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2019-05-11
      • 1970-01-01
      相关资源
      最近更新 更多