AlertView按钮到特定的ViewController

时间:2015-04-23 19:37:27

标签: xcode viewcontroller alertview

我正在研究如何进行AlertView,当按下“确定”按钮时,它会将您带到一个名为DiveNumberViewController的特定视图控制器。

我完成了AlertView代码(见下文),但无法弄清楚如何将OK按钮添加到DiveNumberViewController。任何帮助表示赞赏。

我正在使用Xcode 6.3和Swift

var Alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "OK")
        Alert.show()

2 个答案:

答案 0 :(得分:2)

请改为尝试:

//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

    //index of cancel button
    if buttonIndex == 0
    {
        //add code if needed
    }
    //index of OK button
    if buttonIndex == 1
    {
        //add code to go to your controller
        var divenumberViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DiveNumberViewController") as! DiveNumberViewController

        self.navigationController?.pushViewController(divenumberViewController, animated: true)
    }
}

另外,请检查您的故事板,确保您的控制器类和故事板ID设置在里面。

自定义类| class:DiveNumberViewController

身份|故事板ID:DiveNumberViewController

这里的例子:

enter image description here

答案 1 :(得分:0)

你可以这样做:

var alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles:"OK")
alert.show()

var alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "Cancel")
alert.addButtonWithTitle("OK")
alert.show()

别忘了添加代表

MyController : UIViewcontroller, UIAlertViewDelegate

当用户点击按钮时,代表将在下面触发此功能。所以在这里添加你的代码去DiveNumberViewController

//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

            //index of cancel button
            if buttonIndex == 0
            {
                //add code if needed
            } 
            //index of OK button
            if buttonIndex == 1
            {
                //add code to go to your controller
            }
        }
}

如果你在同一个控制器中使用了很多UIAlertView,你可以为每个UIAlertView添加一个标签。它将允许根据单击的UIAlertView添加特定操作

alertView.tag = 999

有关更多信息,请查看Apple的文档: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html