[iOS Swift4]如何备份ViewController(模态和推送)

时间:2019-05-11 18:28:33

标签: ios swift

[观看次数]
Main.storyboard-ViewController
↓推
Second.storyboard-SecondViewController
↓模态
Third.storyboard-ThirdViewController

我一直在尝试从ThirdViewController→SecondViewController→ViewController

返回

从ThirdViewController返回SecondViewController没问题。

   // ThirdViewController
    @IBAction func tapBackBtn(_ sender: Any) {   
        self.dismiss(animated: true, completion: nil)
        SecondViewController().back()
    }

但是func back()-self.navigationController?.popViewController(animated:true)不起作用。

   // SecondViewController
    func back() {
    // Come here though...
     self.navigationController?.popViewController(animated: true)
    }

    @IBAction func tapBackBtn(_ sender: Any) {
        // It works though...
        self.navigationController?.popViewController(animated: true)
    }

有什么方法可以自动从Third / SecondViewController返回到ViewController吗?
谢谢。

4 个答案:

答案 0 :(得分:1)

它不起作用,因为实际上您通过SecondViewController().back()调用了SecondViewController的初始化方法,并创建了视图控制器的新实例。该实例与您要隐藏的导航堆栈中的实例不同。因此,您可以在实际上不在导航堆栈且没有导航控制器的实例处调用back函数。

要解决此问题,您可以在窗口层次结构中找到这样的导航控制器:

@IBAction func tapBackBtn(_ sender: Any) {   
    self.dismiss(animated: true) {
         guard navigationControlller = UIApplication.shared.windows.first?.rootViewController as? UINavigationController else { return }
         navigationController?.popViewController(animated: true)
    }
}

或者更好的解决方案是重新考虑您的应用程序架构,并将导航职责分配给某些协调的视图控制器。因此,协调员将知道ThirdViewController应该何时消失,并在需要时在popViewController处调用NavigationController

答案 1 :(得分:0)

您要返回第一个视图控制器; aka root

self.navigationController?.popToRootViewController(animated: true)

您还可以弹出一个特定的控制器:

self.navigationController?.popToViewController(aController, animated: true)

答案 2 :(得分:0)

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

将此代码添加到第三个视图控制器的backButton中,它将带您到第一个ViewController

答案 3 :(得分:-1)

首先,您不需要在ThirdViewController的tapBackBtn方法中执行SecondViewController().back(),因为该代码什么也不做。

接下来,如果您在第一个ViewController中执行push,则应通过点击导航控制器中的后退按钮来实现默认的pop实现。

以下是简单执行任务的示例:

// FirstViewController

@IBAction func showButtonDidPress(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Second", bundle: nil)
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
    navigationController?.pushViewController(secondViewController, animated: true)
}

// SecondViewController

@IBAction func presentButtonDidPress(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Third", bundle: nil)
    let thirdViewController = storyboard.instantiateViewController(withIdentifier: "ThirdViewController")
    present(thirdViewController, animated: true, completion: nil)
}

// ThirdViewController

@IBAction func dismissButtonDidPress(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

正如您在这里看到的,我没有使用任何segue,也使用了单独的情节提要。

此外,您可以使用UIStoryboard的简单扩展来使代码更漂亮:

extension UIStoryboard {
    static var main: UIStoryboard {
        return UIStoryboard(name: "Main", bundle: nil)
    }

    static var second: UIStoryboard {
        return UIStoryboard(name: "Second", bundle: nil)
    }

    static var third: UIStoryboard {
        return UIStoryboard(name: "Third", bundle: nil)
    }
}

因此,要创建ViewController的新实例,您将执行以下操作:

let secondViewController = UIStoryboard.second.instantiateViewController(withIdentifier: "SecondViewController")