选择TableView Cell后,将数据传递回Swift中的上一个View Controller

时间:2015-06-04 21:24:40

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

似乎有很多在线教程,但我找不到一个将值从TableView控制器传递回第一个View Controller的教程。

这是我的设置......第一个ViewController有一个文本字段,当用户按下它时,它会将它们带到第二个TableViewController,在那里他们可以从列表中选择一个国家。一旦他们选择了它,我希望他们按导航栏上的DONE按钮,然后我希望它回到第一个ViewController并显示他们在文本字段中选择的国家(准备他们按下按钮和应用程序继续等...)

从我可以解决的问题来看,我需要在DidSelectRowAtIndexPath方法中做到这一点......?但是我也一直在谈论放松赛段?对不起,我是Swift和iOS代码的新手,所以我们希望得到一个明确的答案,即最好和最简单的方法。请参阅下面的代码,了解每个控制器:

的ViewController

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
}

}

提前致谢!

4 个答案:

答案 0 :(得分:0)

在第二个视图控制器(表视图控制器)中添加包含先前视图控制器的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
...
}

答案 1 :(得分:0)

第一个视图控制器

导入 UIKit

class ViewController: UIViewController, UITextFieldDelegate {

if (msg.substring(3, 4) == undefined) { ... }

}

扩展视图控制器:CountrySelectionDelegate {

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

}

答案 2 :(得分:-1)

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

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

我不记得如果你把发件人变量添加到IBAction,你可以从中检索信息。

答案 3 :(得分:-1)

第二个视图控制器

导入 UIKit

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

相关问题