Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Swift iOS向UIAlertView按钮添加操作_Ios_Uialertview_Swift - Fatal编程技术网

如何使用Swift iOS向UIAlertView按钮添加操作

如何使用Swift iOS向UIAlertView按钮添加操作,ios,uialertview,swift,Ios,Uialertview,Swift,我想添加另一个按钮,而不是“确定”按钮,该按钮应该只关闭警报。 我想让另一个按钮调用某个函数 var logInErrorAlert: UIAlertView = UIAlertView() logInErrorAlert.title = "Ooops" logInErrorAlert.message = "Unable to log in." logInErrorAlert.addButtonWithTitle("Ok") 如何在此警报中添加另一个按钮,然后允许它在单击后调用函数,以便我们希

我想添加另一个按钮,而不是“确定”按钮,该按钮应该只关闭警报。 我想让另一个按钮调用某个函数

var logInErrorAlert: UIAlertView = UIAlertView()
logInErrorAlert.title = "Ooops"
logInErrorAlert.message = "Unable to log in."
logInErrorAlert.addButtonWithTitle("Ok")
如何在此警报中添加另一个按钮,然后允许它在单击后调用函数,以便我们希望新按钮调用:

 retry()

UIAlertView
使用代理与您,即客户沟通

添加第二个按钮,然后创建一个对象以从视图接收代理消息:

class LogInErrorDelegate : UIAlertViewDelegate {

    init {}

    // not sure of the prototype of this, you should look it up
    func alertView(view :UIAlertView, clickedButtonAtIndex :Integer) -> Void {
        switch clickedButtonAtIndex {
            case 0: 
               userClickedOK() // er something
            case 1:
               userClickedRetry()
              /* Don't use "retry" as a function name, it's a reserved word */

            default:
               userClickedRetry()
        }
    }

    /* implement rest of the delegate */
}
logInErrorAlert.addButtonWithTitle("Retry")

var myErrorDelegate = LogInErrorDelegate()
logInErrorAlert.delegate = myErrorDelegate

快速的方法是使用新的UIAlertController和闭包:

    // Create the alert controller
    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        NSLog("OK Pressed")
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    // Present the controller
    self.presentViewController(alertController, animated: true, completion: nil)
Swift 3:

    // Create the alert controller
    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
        UIAlertAction in
        NSLog("OK Pressed")
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    // Present the controller
    self.present(alertController, animated: true, completion: nil)
请参阅我的代码:

 @IBAction func foundclicked(sender: AnyObject) {

            if (amountTF.text.isEmpty)
            {
                let alert = UIAlertView(title: "Oops! Empty Field", message: "Please enter the amount", delegate: nil, cancelButtonTitle: "OK")

                alert.show()
            }

            else {

            var alertController = UIAlertController(title: "Confirm Bid Amount", message: "Final Bid Amount : "+amountTF.text , preferredStyle: .Alert)
            var okAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default) {
                UIAlertAction in

                JHProgressHUD.sharedHUD.loaderColor = UIColor.redColor()
                        JHProgressHUD.sharedHUD.showInView(self.view, withHeader: "Amount registering" , andFooter: "Loading")
                }
            var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
                UIAlertAction in

                alertController .removeFromParentViewController()
            }
                            alertController.addAction(okAction)
                            alertController.addAction(cancelAction)

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

            }

        }
根据swift:

let alertCtr = UIAlertController(title:"Title", message:"Message", preferredStyle: .Alert)
let Cancel = AlertAction(title:"remove", style: .Default, handler: {(UIAlertAction) -> Void in })
let Remove = UIAlertAction(title:"remove", style: .Destructive, handler:{(UIAlertAction)-> Void 
    inself.colorLabel.hidden = true    
})
alertCtr.addAction(Cancel)
alertCtr.addAction(Remove)

self.presentViewController(alertCtr, animated:true, completion:nil)}

杰克答案的Swift 3.0版本

//创建警报控制器

let alertController = UIAlertController(title: "Alert!", message: "There is no items for the current user", preferredStyle: .alert)

            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }

            // Add the actions
            alertController.addAction(okAction)
            alertController.addAction(cancelAction)

            // Present the controller
            self.present(alertController, animated: true, completion: nil)

Swift 4更新

        // Create the alert controller
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }

        // Add the actions
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)

        // Present the controller
        self.present(alertController, animated: true, completion: nil)

这是针对swift 4.2、5和5的+

let alert = UIAlertController(title: "ooops!", message: "Unable to login", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))

self.present(alert, animated: true)
斯威夫特5+

let alert = UIAlertController(title: "Cart!", message: "Are you sure want to remove order details?", preferredStyle: .alert)
        // Create the actions
let okAction = UIAlertAction(title: "YES", style: 
    UIAlertAction.Style.default) {
       UIAlertAction in
       print("Yes Pressed")
}
let cancelAction = UIAlertAction(title: "CANCEL", style: 
    UIAlertAction.Style.cancel) {
       UIAlertAction in
       print("Cancel Pressed")
    }
// Add the actions
alert.addAction(okAction)
alert.addAction(cancelAction)

self.present(alert, animated: true, completion: nil)

不幸的是,我目前正在使用iOS 7.1,而UIAlertController仅在iOS 8.0+中可用。因此,我必须使用UIAlertViewI。我的做法与您完全相同,但错误不会出现,而是在日志中显示“警告,尝试显示不在窗口层次结构中的视图!”知道问题出在哪里吗?在我的情况下,会出现警报,但按钮不会响应触摸。也就是说,“按确定”和“按取消”都不会显示。UIAlertActionStyle.default将重命名为UIAlertAction.Style.default
let alert = UIAlertController(title: "Cart!", message: "Are you sure want to remove order details?", preferredStyle: .alert)
        // Create the actions
let okAction = UIAlertAction(title: "YES", style: 
    UIAlertAction.Style.default) {
       UIAlertAction in
       print("Yes Pressed")
}
let cancelAction = UIAlertAction(title: "CANCEL", style: 
    UIAlertAction.Style.cancel) {
       UIAlertAction in
       print("Cancel Pressed")
    }
// Add the actions
alert.addAction(okAction)
alert.addAction(cancelAction)

self.present(alert, animated: true, completion: nil)