簡體   English   中英

解開序幕后,ViewController仍然停留

[英]ViewController still stays after unwind segue

因此,我有多個控制器,我們稱它們為A,B和C。

當我轉到A並在表視圖中選擇一個項目時,我被傳送到B,可以在其中編輯項目並保存。 現在,當我按保存時,我有一個輕松的選擇,可將我帶到主頁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:

在此處輸入圖片說明

所以當我按保存時,它會將我傳送到viewcontroller C:這就是viewcontrollerC:

在此處輸入圖片說明

然后,使用選項卡欄,當我回到viewcontroller a時,我對此表示歡迎:

在此處輸入圖片說明

如您所見,選項卡在第三個選項上,但是我對Viewcontroller B表示歡迎,然后,我必須再次單擊第三個選項(一個標題為collection)以返回到viewcontrollerA。

希望事情變得更清楚,請告訴我是否需要任何代碼。

如果這是您需要的,請嘗試以下操作:

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

}

如果您有任何疑問,請隨時。 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM