如何在我的Swift应用程序中向用户显示我的UIViewController是否被查看?

时间:2022-12-12 07:11:15

I have a UIViewController with a button. This button has a segue connected to another UIViewController and the segue is of type Show. It looks somehow like this:

我有一个带按钮的UIViewController。这个按钮有一个segue连接到另一个UIViewController,而segue是Show类型。它看起来像这样:

self.performSegueWithIdentifier("myIdentifier", sender: user)

and the function prepareForSegue is:

而函数prepareForSegue是:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "myIdentifier"){

        if let frD = segue.destinationViewController as? MyNextClass,
        ...

Now when user presses the button, the new UIViewController pops out on fullscreen. It also has a button called exitButton that does one thing:

现在,当用户按下按钮时,新的UIViewController将在全屏幕上弹出。它还有一个名为exitButton的按钮,可以执行以下操作:

@IBAction func exitbuttonaction(sender: AnyObject) {

    self.dismissViewControllerAnimated(true, completion: nil)
}

When user presses it - they dismiss this view controller and they see the previous one. That previous container has a viewWillAppear function, but it doesn't get called each time user comes back to it from the 2nd controller. Why not, since it appears every time to the user?

当用户按下它时 - 它们会关闭此视图控制器并看到前一个视图控制器。前一个容器有一个viewWillAppear函数,但每次用户从第二个控制器返回时都不会调用它。为什么不,因为它每次出现在用户身上?

Also, is there any other way of distinguishing when the parent controller appeared to the user? (maybe some other function similar to viewWillAppear that would work while dismissing the 2nd controller)

此外,还有其他方法可以区分父控制器何时出现在用户身上? (可能是一些类似于viewWillAppear的其他函数,在解散第二个控制器时可以工作)

1 个解决方案

#1


1  

You must use an UnwindSegue. An UnwindSegue gives you a way to “unwind” the navigation stack and specify a destination to go back to. Your viewWillAppear never call again because already appeared.

您必须使用UnwindSegue。 UnwindSegue为您提供了一种“展开”导航堆栈并指定要返回的目标的方法。你的viewWillAppear从未再次打电话,因为已经出现了。

If you want to know what a UnwindSegue is, you can check this answer: * anser

如果你想知道UnwindSegue是什么,你可以检查这个答案:* anser

If you want to know how to do it, I recommend these links:

如果您想知道如何操作,我推荐这些链接:

apple documentation about unwind segues

关于展开segues的苹果文档

tutorial about unwind segues

关于展开segues的教程

youtube videotutorial

#1


1  

You must use an UnwindSegue. An UnwindSegue gives you a way to “unwind” the navigation stack and specify a destination to go back to. Your viewWillAppear never call again because already appeared.

您必须使用UnwindSegue。 UnwindSegue为您提供了一种“展开”导航堆栈并指定要返回的目标的方法。你的viewWillAppear从未再次打电话,因为已经出现了。

If you want to know what a UnwindSegue is, you can check this answer: * anser

如果你想知道UnwindSegue是什么,你可以检查这个答案:* anser

If you want to know how to do it, I recommend these links:

如果您想知道如何操作,我推荐这些链接:

apple documentation about unwind segues

关于展开segues的苹果文档

tutorial about unwind segues

关于展开segues的教程

youtube videotutorial