ios 在 Swift 中创建一个 UIAlertAction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28137259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:29:58  来源:igfitidea点击:

Create an UIAlertAction in Swift

iosswiftuialertcontroller

提问by Cesare

I would like to create the following UIAlertAction:

我想创建以下内容UIAlertAction

img1

图像1

@IBAction func buttonUpgrade(sender: AnyObject) {

           let alertController = UIAlertController(title: "Title",
        message: "Message",
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
        // ...
    }

    alertController.addAction(cancelAction)
}

I'm aware that an UIAlertControlleris initialized with a title, message, and whether it prefers to be displayed as an alert or action sheet.

我知道 anUIAlertController是用title,初始化的,message以及它是否更喜欢显示为警报或操作表。

When the button is pressed I would like to display the alert, but alert.show()doesn't work. Why doesn't my code work?

当按下按钮时,我想显示警报,但alert.show()不起作用。为什么我的代码不起作用?

回答by Mick MacCallum

The main issue here is that UIAlertController(unlike UIAlertView) is a subclass of UIViewControlller, meaning it needs to be presented as such (and not via the show()method). Other than that, if you want to change to color of the cancel button to red, you have to set the cancel action's alert style to .Destructive.

这里的主要问题是UIAlertController(与 不同UIAlertView)是 的子类UIViewControlller,这意味着它需要这样呈现(而不是通过show()方法)。除此之外,如果要将取消按钮的颜色更改为红色,则必须将取消操作的警报样式设置为.Destructive

This only works if you want the button to be red. If you want to change the colors of the buttons in the alert controller to arbitrary colors, this can only be done by setting the tintColorproperty on the alert controller's viewproperty, which will change the tint color of all of its buttons (except those that are destructive). It should be noted that with the design paradigms that Apple has put in place, it isn't necessary to change the cancel button's color due to the implications of its having bolded text.

这仅在您希望按钮为红色时才有效。如果您想将警报控制器中按钮的颜色更改为任意颜色,则只能通过设置tintColor警报控制器属性上的view属性来完成,这将更改其所有按钮的色调颜色(破坏性按钮除外) )。应该注意的是,根据 Apple 已经实施的设计范例,由于其具有粗体文本的含义,没有必要更改取消按钮的颜色。

If you do still want the text to be red though, it can be done like this:

如果您仍然希望文本为红色,则可以这样做:

let alertController = UIAlertController(
    title: "Title",
    message: "Message",
    preferredStyle: UIAlertControllerStyle.Alert
)

let cancelAction = UIAlertAction(
    title: "Cancel",
    style: UIAlertActionStyle.Destructive) { (action) in
    // ...
}

let confirmAction = UIAlertAction(
    title: "OK", style: UIAlertActionStyle.Default) { (action) in
    // ...
}

alertController.addAction(confirmAction)
alertController.addAction(cancelAction)

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

Which produces the results you're after:

这会产生您所追求的结果:

enter image description here

在此处输入图片说明

回答by RonM

let alertController = UIAlertController(
    title: "Title",
    message: "Message",
    preferredStyle: UIAlertControllerStyle.Alert
)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
    // ...
}

let okayAction = UIAlertAction(title: "OK", style: .Default) { (action) in
    // ...
}

alertController.addAction(okayAction)    
alertController.addAction(cancelAction)

self.presentViewController(alertController, animated: true) {
    // ...
}

回答by Armin Scheithauer

var alertController = UIAlertController(title: "Alert", message:"Message", preferredStyle: UIAlertControllerStyle.Alert)
let confirmed = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

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

Important is the last line "self.presentViewController" to actually show your alert.

重要的是最后一行“self.presentViewController”来实际显示您的警报。

Hope it works ;)

希望它有效;)