Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检测到按下back键以访问当前视图控制器_Ios_Swift - Fatal编程技术网

Ios 检测到按下back键以访问当前视图控制器

Ios 检测到按下back键以访问当前视图控制器,ios,swift,Ios,Swift,我正在使用导航控制器使用下面的代码从一个视图返回到上一个视图 ChildViewController.swift: self.navigationController.popViewControllerAnimated(true) func backWasPressed(viewControllerIdentifier: String!) { // if back was pressed from this view controller and not from any other

我正在使用导航控制器使用下面的代码从一个视图返回到上一个视图

ChildViewController.swift:

self.navigationController.popViewControllerAnimated(true)
func backWasPressed(viewControllerIdentifier: String!) {
    // if back was pressed from this view controller and not from any other view
    if viewControllerIdentifier == "ChildViewController" {
            // do stuff here
    }
}
我需要一种方法来检测导航控制器是否转到实际的上一个视图,如下图所示

ParentViewController.swift:

self.navigationController.popViewControllerAnimated(true)
func backWasPressed(viewControllerIdentifier: String!) {
    // if back was pressed from this view controller and not from any other view
    if viewControllerIdentifier == "ChildViewController" {
            // do stuff here
    }
}

还有什么方法可以做到这一点吗?

看看你不需要知道这一点。你可能认为你有,但你没有。整个提议的架构都是似是而非的:

func backWasPressed(viewControllerIdentifier: String!) {
    // if back was pressed from this view controller and not from any other view
    if viewControllerIdentifier == "ChildViewController" {
            // do stuff here
    }
}

如果推送的视图控制器有一些信息要与堆栈下一层的视图控制器通信,则弹出该视图控制器时,这就是推送的视图控制器的作业。它知道它正在被弹出,并且知道如何访问另一个视图控制器(如果对此有任何疑问,您可以使用代理体系结构),因此问题就以这种方式得到了妥善解决。这与演示视图控制器需要在解聘时与演示者通信的情况完全相同。

UIViewController
有相应的方法。在
视图将出现
方法(或
视图显示
)中,使用
isMovingToParentViewController
方法。如果为false,则显示视图控制器是因为前一个视图控制器被弹出。@rmaddy,这只是我需要它告诉我的内容的一半。isMovingToParentViewController不会告诉我用户是从哪里回来的。在我的应用程序中,有多个ChildViewController返回到同一个ParentViewController。在返回到ParentViewController之前,我需要明确知道使用了哪个ChildViewController。
UIViewController
无法告诉您这一点。你需要自己记录下来。