繁体   English   中英

何时/如何使用dismissViewControllerAnimated? 它可以与UIWindow中的Controller一起使用吗?

[英]When/How to use dismissViewControllerAnimated? Does it work with Controller from UIWindow?

这些问题来自一个答案,我从另一个问题了这里

我想知道下面注释的函数调用有关系吗? 我试图运行该应用程序并关闭警报,但从未调用dismissViewControllerAnimated。

有人可以在何时/如何使用该功能方面分享一些经验吗?

class GameViewController: UIViewController {
    @IBOutlet var gameBoardUIView: GameBoardUIView
    ...
}

class GameBoardUIView: UIView {
    ...
    func move() {
        if !gameBoard.checkNextMoveExist() {
            let parentViewController: UIViewController = UIApplication.sharedApplication().windows[1].rootViewController

            var alert = UIAlertController(title: "Game Over", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Take Me Back", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
                // parentViewController.dismissViewControllerAnimated(true, completion: nil)
                println("Taking user back to the game without restarting")
            }))
            alert.addAction(UIAlertAction(title: "New Game", style: UIAlertActionStyle.Destructive, handler: {(action: UIAlertAction!) in
                // parentViewController.dismissViewControllerAnimated(true, completion: nil)
                println("Starting a new game")
                self.restartGame()
            }))
            parentViewController.presentViewController(alert, animated: true, completion: nil)
        }
    }
}

编辑:对我上面的示例一个更具体的问题。 当我单击警报上的按钮时,为什么dismissViewControllerAnimated没有执行任何操作? 消息(例如“开始新游戏”)已成功打印。 是因为我从UIWindow数组获得了控制器吗?

它基本上消除了堆栈上的当前ViewController并显示了它来自的先前ViewController。 当您想回到原先的View Controller时使用此方法。

编辑:

它可能什么也不做的原因可能是因为您必须在处理AlertView Button单击事件时使用某种方法或委托。 否则,它将作为空白单击并停留在原处。 我不知道Swift中的方法,但是在Xcode中有一个方法叫做:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //your code for alert button click
}

这可以处理您要在“警报按钮”单击上执行的操作。

希望这可以帮助。

暂无
暂无

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

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