简体   繁体   English

解开序幕后,ViewController仍然停留

[英]ViewController still stays after unwind segue

So I have multiple controllers, lets call them A, B and C. 因此,我有多个控制器,我们称它们为A,B和C。

When I go to A, and select an Item in the tableview, I get transported to B where I can edit the items and save. 当我转到A并在表视图中选择一个项目时,我被传送到B,可以在其中编辑项目并保存。 Now, when I press save, I have an unwind segue which takes me to C, the home page. 现在,当我按保存时,我有一个轻松的选择,可将我带到主页C。

However, when I go to A again, the view is still stuck on B and I have to press the tabBarItem again which I used to come to A again, to go back to A. Why does this happen, I just want A to appear again and B to dissapear as soon as I press save. 但是,当我再次进入A时,视图仍然停留在B上,我必须再次按一下曾经回到A的tabBarItem,然后回到A。为什么会这样,我只想让A出现再按一次,B消失。

I have tried code such as: 我已经尝试过以下代码:

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

in both the segue and the save button code, but nothing seems to happen. 在segue和保存按钮代码中,但似乎什么也没有发生。

Ok, here is more context as requested: 好的,这是根据要求提供的更多上下文:

So this is ViewController A: 这是ViewController A:

在此处输入图片说明

and this is viewController B: 这是viewController B:

在此处输入图片说明

so when I press save, it transports me to viewcontroller C: which is this and this is viewcontrollerC: 所以当我按保存时,它会将我传送到viewcontroller C:这就是viewcontrollerC:

在此处输入图片说明

and then, using the tab bar, when I go back to viewcontroller a, I get greeted with this: 然后,使用选项卡栏,当我回到viewcontroller a时,我对此表示欢迎:

在此处输入图片说明

as you can see, the tabbar is on the third option but I am getting greeted with viewcontroller B and then, I have to click on the third option again(the one titled collection) to go back to viewcontroller A. 如您所见,选项卡在第三个选项上,但是我对Viewcontroller B表示欢迎,然后,我必须再次单击第三个选项(一个标题为collection)以返回到viewcontrollerA。

Hope that makes things clearer, please do let me know if any code is needed. 希望事情变得更清楚,请告诉我是否需要任何代码。

If this is what you need, try this: 如果这是您需要的,请尝试以下操作:

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

}

If you have some question feel free. 如果您有任何疑问,请随时。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM