ios 如何向 UIAlertController 添加操作并获取操作结果 (Swift)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38042028/
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 09:34:19  来源:igfitidea点击:

How to add actions to UIAlertController and get result of actions (Swift)

iosxcodeswiftuialertcontroller

提问by Abhi V

I want to set up a UIAlertControllerwith four action buttons, and the titles of the buttons to be set to "hearts", "spades", "diamonds", and "clubs". When a button is pressed, I want to return its title.

我想设置一个UIAlertController带有四个动作按钮的按钮,按钮的标题设置为“红心”、“黑桃”、“钻石”和“俱乐部”。当按下按钮时,我想返回其标题。

In short, here is my plan:

简而言之,这是我的计划:

// TODO: Create a new alert controller

for i in ["hearts", "spades", "diamonds", "clubs"] {

    // TODO: Add action button to alert controller

    // TODO: Set title of button to i

}

// TODO: return currentTitle() of action button that was clicked

回答by Pranav Wadhwa

Try this:

尝试这个:

let alert = UIAlertController(title: "Alert Title", message: "Alert Message", style = .Alert)
for i in ["hearts", "spades", "diamonds", "hearts"] {
    alert.addAction(UIAlertAction(title: i, style: .Default, handler: doSomething)
}
self.presentViewController(alert, animated: true, completion: nil)

And handle the action here:

并在此处处理操作:

func doSomething(action: UIAlertAction) {
    //Use action.title
}

For future reference, you should take a look at Apple's Documentation on UIAlertControllers

为了将来参考,您应该查看Apple 在 UIAlertControllers 上的文档

回答by Ulli H

here′s a sample code with two actions plus and ok-action:

这是带有两个动作 plus 和 ok-action 的示例代码:

import UIKit

// The UIAlertControllerStyle ActionSheet is used when there are more than one button.
@IBAction func moreActionsButtonPressed(sender: UIButton) {
    let otherAlert = UIAlertController(title: "Multiple Actions", message: "The alert has more than one action which means more than one button.", preferredStyle: UIAlertControllerStyle.ActionSheet)

    let printSomething = UIAlertAction(title: "Print", style: UIAlertActionStyle.Default) { _ in
        print("We can run a block of code." )
    }

    let callFunction = UIAlertAction(title: "Call Function", style: UIAlertActionStyle.Destructive, handler: myHandler)

    let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)

    // relate actions to controllers
    otherAlert.addAction(printSomething)
    otherAlert.addAction(callFunction)
    otherAlert.addAction(dismiss)

    presentViewController(otherAlert, animated: true, completion: nil)
}

func myHandler(alert: UIAlertAction){
    print("You tapped: \(alert.title)")
}}

with i.E. handler: myHandleryou define a function, to read the result of the let printSomething.

使用 iE handler: myHandler定义一个函数,以读取let printSomething的结果。

This is just oneway ;-)

这只是一种方式;-)

Any questions?

任何问题?