【问题标题】:AlertView button to specific ViewController特定 ViewController 的 AlertView 按钮
【发布时间】:2015-04-23 19:37:26
【问题描述】:

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

我已经完成了 AlertView 代码(见下文),但不知道如何将 OK 按钮添加到 DiveNumberViewController。任何帮助表示赞赏。

我正在使用 Xcode 6.3 和 Swift

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

【问题讨论】:

    标签: xcode viewcontroller alertview


    【解决方案1】:

    试试这个:

    //// 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。

    自定义类 |类 : DiveNumberViewController

    身份 |故事板 ID:DiveNumberViewController

    这里的例子:

    【讨论】:

    • @scubadivingfool 对不起,我无权回答您的评论,所以我的回答是。希望,它有帮助!
    • 复制了所有内容,错误消失了,但它仍然在第一个 ViewController 而不是 DiveNumberViewController 上
    • @Scubadivingfool 你是否使用 UINavigationController,它有你的 viewcontroller(带有 UIAlertView 的控制器)作为 rootController?如果没有,这是正常的,它不起作用,因为它是 UINavigationController,它允许您转到 DiveNumberViewController。查看这些链接以了解其工作原理:developer.apple.com/library/ios/documentation/UIKit/Reference/…ioscreator.com/tutorials/…
    • 我目前正在使用 TabViewController。我将 AlertViewViewController 作为第一个选项卡,将 DiveNumberViewController 作为第二个选项卡。这里的代码在 AlertViewViewController
    • @Scubadivingfool 好的!所以你在这里有两个解决方案:1.如果你想在用户点击“OK”时转到第二个标签栏(它有 DiveNumberViewController):使用这个:“self.tabBarController?.selectedIndex = 1”2.如果你想管理第一个标签栏中的所有内容:添加一个 UINavigationcontroller 作为第一个标签栏,并且这个 UINavigationController 必须作为根控制器:AlertViewViewController。您将能够使用 self.navigationController?.pushViewController
    【解决方案2】:

    你可以这样做:

    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

    【讨论】:

    • 对不起,我的评论是上面的答案
    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多