ios - 从 UIAlertController 按钮导航到 ViewController 单击

标签 ios swift uiviewcontroller xcode6 uialertcontroller

我对 Swift 开发还很陌生,我尝试引用 Swift UIAlertController API,但无法弄清楚如何在单击 UIAlertController 上的按钮后导航到另一个 UIViewController。

如果有任何指示、帮助或解决此问题的方法,我将不胜感激。我的代码片段如下 -

@IBAction func showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)


    let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
    alertController.addAction(declineAction)

    let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

        let secondVC = ViewController(nibName: "ViewController", bundle: nil)
        let navController = UINavigationController(rootViewController: secondVC)
        self.presentViewController(navController, animated: true, completion: nil)

    }
    alertController.addAction(acceptAction)

    presentViewController(alertController, animated: true, completion: nil)

}

我在这里想做的是,当单击按钮时,会显示免责声明 AlertController。如果选择“拒绝”按钮,则执行取消操作;如果选择“接受”,应用程序应导航到导航 Controller ,然后该 Controller 允许我导航到其他 ViewController使用应用程序中的菜单。

之前,我使用 Storyboard将一个按钮链接到 NavigationController,以遍历到我想要的 ViewController。现在我想以编程方式对 AlertController “Accept” 按钮执行相同的操作。

提前谢谢您。

最佳答案

您需要实现处理程序 block 以在选择操作时执行代码:

@IBActionfunc showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)


    let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
    alertController.addAction(declineAction)

    let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

        let secondVC = SecondViewController(nibName: "SecondView", bundle: nil)
        let navController = UINavigationController(rootViewController: secondVC)
        self.presentViewController(navController, animated: true, completion: nil)
    }
    alertController.addAction(acceptAction)

    presentViewController(alertController, animated: true, completion: nil)

}

关于ios - 从 UIAlertController 按钮导航到 ViewController 单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28437104/

相关文章:

ios - 当用户未登录 iCloud 时,我可以在使用 CloudKit 的应用程序中使用订阅吗?

ios - 使用 6 位数字(而不是 4 位)同步短信验证

ios - iPhone 的 UIViewController 大小不正确

ios - 使用导航栏后退按钮返回主视图

ios - swiftc 失败,错误代码为 1

objective-c - 将不同方向的 UIViewController 推送到上一个

ios - 创建触发警报 View Controller 的选项卡栏项目

ios - AVAssetExportSession 卡住(未启动)导出

ios - 在 iOS 应用程序中使用@synthesize

xcode - 为什么在这种情况下我的布局约束会返回错误? ( swift )