如何向 UIAlertController 添加操作并获取操作结果 (Swift)
How to add actions to UIAlertController and get result of actions (Swift)
我想设置一个 UIAlertController
有四个操作按钮,按钮的标题设置为 "hearts"、"spades"、"diamonds" 和"clubs"。当按下一个按钮时,我想 return 它的标题。
简而言之,这是我的计划:
// 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
这是一个示例代码,其中包含两个操作 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)")
}}
与即handler: myHandler 你定义一个函数,来读取结果
让 printSomething.
这只是一种方式;-)
有问题吗?
试试这个:
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)
并在此处处理操作:
func doSomething(action: UIAlertAction) {
//Use action.title
}
为了将来参考,您应该看看 Apple's Documentation on UIAlertControllers
我想设置一个 UIAlertController
有四个操作按钮,按钮的标题设置为 "hearts"、"spades"、"diamonds" 和"clubs"。当按下一个按钮时,我想 return 它的标题。
简而言之,这是我的计划:
// 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
这是一个示例代码,其中包含两个操作 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)")
}}
与即handler: myHandler 你定义一个函数,来读取结果 让 printSomething.
这只是一种方式;-)
有问题吗?
试试这个:
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)
并在此处处理操作:
func doSomething(action: UIAlertAction) {
//Use action.title
}
为了将来参考,您应该看看 Apple's Documentation on UIAlertControllers