将功能添加到alertcontroller中的按钮

时间:2014-11-01 09:55:20

标签: ios swift uialertcontroller

我是swift的新手,我在警报视图课程中学习了一些教程。我想添加函数就像调用另一个viewcontroller,只要点击警报按钮但我不知道如何。所以请帮助我

    func showAlertController(){

    var title : String = "hi!"
    var message : String = NSLocalizedString("Are you feeling well? ", comment:"")
    let cancelButtonTitle = NSLocalizedString("No", comment:"")
    let otherButtonTitle = NSLocalizedString("Yes", comment:"")

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

    let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){
        action in NSLog("No!!!!")
    }

    let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){
        action in NSLog("welcome!!! hello back")
    }

    alertController.addAction(cancelAction)
    alertController.addAction(otherAction)
    presentViewController(alertController, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:1)

试试这段代码:

let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){
        action in NSLog("No!!!!")
        let View2 = self.storyboard?.instantiateViewControllerWithIdentifier("View2") as TwoViewController
        self.navigationController?.pushViewController(View2, animated: true)

    }

并选择现有的视图控制器,然后从下拉菜单中选择编辑>嵌入>导航控制器

之后添加一个新的视图控制器并创建一个新的Cocoa类,并将其命名为 TwoViewController UIViewController的子类。

之后选择新的ViewController并从Identity Inspector enter image description here

以这种方式自定义它

可能这可以帮到你。