是否可以将AlertViewController一个接一个地呈现?

时间:2015-11-16 19:37:22

标签: ios swift user-interface uialertview

我想要一个UIAlertViewController来显示一些文字,当你点击"确定"它会导致另一个UIAlertController弹出,它会向用户显示两个可供选择的选项(转到主,重启游戏)。这是我写的代码,它带来了这个错误:

  

" 2015-11-16 22:29:21.438 MemoryCardGameTest_01 [1917:46708]警告:   尝试出席    已经是   提出(null)"

 func showWinMessage() {

    let userMessage = UIAlertController(title: "Victory! you win with... clicks \(ref2.clickCounter) and \(ref2.timeCounter) seconds", message: "", preferredStyle: UIAlertControllerStyle.Alert);

    let action = UIAlertAction(title: "ok", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in

    }

    userMessage.addAction(action);

    self.presentViewController(userMessage, animated: true, completion: nil);


    println("victory achieved");


}

func showUserMessage(){
    let newMessage = UIAlertController(title: "whatwouldyouliketodonext?", message: "", preferredStyle: UIAlertControllerStyle.Alert);

    let goToUI = UIAlertAction(title: "goToUI", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
        println("gotoUI was clicked");

    }

    let playAgain = UIAlertAction(title: "PlayAgain", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
        println("playAgain was clicked");
    }

    newMessage.addAction(goToUI);
    newMessage.addAction(playAgain);

    self.presentViewController(newMessage, animated: true, completion: nil);


}

1 个答案:

答案 0 :(得分:2)

这大致可以满足您的需求。只要确保你没有在viewDidLoad中调用它,因为你可能会收到像你提到的错误。试试viewDidAppear或其他什么。

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let alertController = UIAlertController(title: "First message", message: "This is the first message", preferredStyle: .Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
        // ...
    }
    alertController.addAction(cancelAction)

    let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
        let alertController = UIAlertController(title: "Second message", message: "This is the second message", preferredStyle: .Alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
            // ...
        }
        alertController.addAction(cancelAction)

        let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
            // ...
        }
        alertController.addAction(OKAction)

        self.presentViewController(alertController, animated: true) {
            // ...
        }
    }
    alertController.addAction(OKAction)

    self.presentViewController(alertController, animated: true) {
        // ...
    }
}

基本上我只是将第二个视图设置为出现在第一个alertController的OKAction中。