如何使用Container对VC执行segue

时间:2017-05-12 14:48:34

标签: ios swift segue

see this image

see this gif

当我选择城市Med时,它传递给TableVC而不是FirstVC(MainVC)

我能这样做吗?通过传递的数据进入mainVC  容器(TableVC)?

这是我到目前为止所做的事情

MainVC

TableVC

import UIKit

class passedViewController: UITableViewController {

@IBOutlet weak var passcelltow: UITableViewCell!
@IBOutlet weak var passcell: UITableViewCell!

var passedCity1 = "اختر المدينة الاولى"
var passedCity2 = "اختر المدينة الثانية"
 override func viewDidLoad() {
    super .viewDidLoad()

    passcell.textLabel?.text = passedCity1
    passcelltow.textLabel?.text = passedCity2

}

}

表1包含要传递给TableVC的数据

导入UIKit

class city2ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!

var city2 = ["RUH" , "Med" , "Jed"]

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return city2.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    print(indexPath.row)

    cell.textLabel?.text = city2[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "show", sender: city2[indexPath.row])
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let passing = segue.destination as! passedViewController

    passing.passedCity2 = sender as! String

}




}

表2是相同的..

推荐错误

  

0 1 2无法转换类型的值' UIViewController' (0x107a10288)来   ' table_view_test_pass.passedViewController' (0x105dbfdf8)。 (LLDB)

2 个答案:

答案 0 :(得分:0)

如果你想要转向MainVC,你应该在准备segue时从那个类中实例化一个视图控制器。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let passing = segue.destination as! ViewController
    passing.passedCity2 = sender as! String
}

将ViewController更改为您的类名称适用于MainVC。

答案 1 :(得分:0)

如果你想回到父视图,你应该使用unwind-segue。

为此,你必须在父视图中创建unwind segue方法,如下所示

@IBAction func unwindSegueFromChild(segue: UIStoryboardSegue){
    // This code executes when returning to view
}

在您的子视图中,您必须创建展开segue ctrl +拖动

enter image description here 会出现一个下拉菜单,您可以选择unwindSegueFromChild

完成后,您必须为unwind segue分配一个标识符,并以正常的segue编程方式执行它。