【问题标题】:ViewController still stays after unwind segueViewController 在 unwind segue 后仍然停留
【发布时间】:2018-06-15 22:44:53
【问题描述】:

所以我有多个控制器,我们称它们为 A、B 和 C。

当我转到 A 并在 tableview 中选择一个项目时,我会被传送到 B 在那里我可以编辑项目并保存。现在,当我按下保存时,我有一个 unwind segue 将我带到 C 主页。

但是,当我再次去A时,视图仍然停留在B上,我必须再次按下我曾经再次来到A的tabBarItem,才能回到A。为什么会发生这种情况,我只是想当我按下保存时,A 再次出现,B 消失。

我尝试过如下代码:

self.presentingViewController?.dismiss(animated: false, completion:nil)
self.dismiss(animated: false, completion: nil)

在 segue 和保存按钮代码中,但似乎什么也没发生。

好的,这里是要求的更多上下文:

这是 ViewController A:

这是 viewController B:

所以当我按下保存时,它会将我传送到视图控制器 C:就是这个 这是 viewcontrollerC:

然后,使用标签栏,当我返回 viewcontroller a 时,我收到了这样的问候:

如您所见,选项卡栏位于第三个选项上,但我得到了视图控制器 B 的欢迎,然后,我必须再次单击第三个选项(标题为集合的那个)才能返回到视图控制器 A。

希望能让事情更清楚,如果需要任何代码,请告诉我。

【问题讨论】:

  • 需要更多上下文来回答您的问题。主页是不同的视图控制器吗?它与其他视图控制器有什么关系?观点 A 仍然坚持 B 是什么意思?您是否以模态方式呈现视图控制器?还是将它们中的每一个都推入导航控制器堆栈?
  • 好的,我会在上面的问题中添加更多内容
  • 我在回答中添加了更多细节。

标签: ios swift3 segue dismiss


【解决方案1】:

如果这是你需要的,试试这个:

class CViewController : UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "C"
        view.backgroundColor = .yellow

        let button = UIButton(type: .system)
        button.setTitle("Go to A", for: .normal)
        button.addTarget(self, action: #selector(goToA), for: .touchUpInside)

        button.frame = CGRect(x: 100, y: 200, width: 100, height: 40)

        view.addSubview(button)

    }

    @objc func goToA() {
        let AVC = AViewController()
        self.navigationController?.pushViewController(AVC, animated: true)
    }

}


class AViewController : UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "A"
        view.backgroundColor = .green

        tableView.delegate = self
        tableView.dataSource = self

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "Cell \(indexPath.row)"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let BVC = BViewController()
        self.navigationController?.pushViewController(BVC, animated: true)
    }



}

class BViewController : UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        title = "B"
        view.backgroundColor = .cyan

        let button = UIButton(type: .system)
        button.setTitle("save", for: .normal)
        button.addTarget(self, action: #selector(save), for: .touchUpInside)

        button.frame = CGRect(x: 100, y: 200, width: 100, height: 40)

        view.addSubview(button)


    }

    @objc func save() {
        self.navigationController?.popToRootViewController(animated: true)
    }

}

如果您有任何问题,请随时提出。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多