0

我是 swift 新手,我在警报视图课程中遵循了一些教程。我想添加功能,例如每当单击警报按钮时调用另一个视图控制器,但我不知道如何。所以请帮助我

    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)
}
4

1 回答 1

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)
        
    }

并选择您现有的视图控制器,然后从下拉菜单中选择Edit>Embed in > Navigation Controller

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

之后选择您的新 ViewController 并从 Identity Inspector 以这种方式自定义它在此处输入图像描述

可能这可以帮助你。

于 2014-11-01T10:05:03.963 回答