在开发iOS应用程序时,用户交互非常重要。要创建用于与用户交互的弹出窗口,您需要使用UIAlertController
。UIAlertController
是一个视图控制器,可以帮助您创建弹出窗口,包括警报框和操作表。本文将为您介绍UIAlertController
的基本用法及其各种功能,帮助您创建各种弹出窗口。
一、创建警报框
警报框是在应用程序中显示的一个弹出窗口,用于向用户显示重要信息或确认某些操作。下面的代码将创建一个简单的警报框,其中包括一个文本消息、一个标题以及一个OK按钮。
// 创建警报框
let alertController = UIAlertController(
title: "提示",
message: "这是一条提示信息",
preferredStyle: .alert)
// 添加确定按钮
let okAction = UIAlertAction(
title: "确定",
style: .default,
handler: nil)
alertController.addAction(okAction)
// 显示警报框
self.present(alertController, animated: true, completion: nil)
在上面的代码中,我们使用UIAlertController
的init(title:message:preferredStyle:)
方法来创建一个新的UIAlertController
。preferredStyle
参数指定了要创建的警报框或操作表的类型。在这种情况下,我们使用.alert
样式,这表示我们要创建一个警报框。我们还为UIAlertController
添加了一个标题和一条消息。接下来,我们使用addAction(_ :)
方法将一个名为“确定”的操作添加到UIAlertController
中。这个操作没有指定处理程序,因为它只是关闭警报框。
二、创建操作表
操作表用于向用户显示一组选项,它们可以执行不同的操作。下面的代码将创建一个操作表,其中包括两个选项:“选项1”和“选项2”,以及取消按钮。
// 创建操作表
let alertController = UIAlertController(
title: "选择一项",
message: "请选择其中一项",
preferredStyle: .actionSheet)
// 添加选项1
let option1Action = UIAlertAction(
title: "选项1",
style: .default,
handler: {(alert: UIAlertAction!) -> Void in
print("选择了选项1")
})
alertController.addAction(option1Action)
// 添加选项2
let option2Action = UIAlertAction(
title: "选项2",
style: .default,
handler: {(alert: UIAlertAction!) -> Void in
print("选择了选项2")
})
alertController.addAction(option2Action)
// 添加取消按钮
let cancelAction = UIAlertAction(
title: "取消",
style: .cancel,
handler: {(alert: UIAlertAction!) -> Void in
print("取消")
})
alertController.addAction(cancelAction)
// 显示操作表
self.present(alertController, animated: true, completion: nil)
在上面的代码中,我们使用UIAlertController
的alertController(title:message:preferredStyle:)
方法来创建一个新的UIAlertController
。这一次,我们使用.actionSheet
样式,这表示我们要创建一个操作表。我们还为UIAlertController
添加了一个标题和一条消息。接下来,我们使用addAction(_ :)
方法将名为“选项1”和“选项2”的操作添加到UIAlertController
中。这两个操作都具有处理程序,当用户选择其中一个选项时,将调用处理程序并执行相应的操作。最后,我们使用addAction(_ :)
方法将一个名为“取消”的操作添加到UIAlertController
中,以及样式为.cancel
,这将关闭操作表。
三、处理用户输入
创建警报框简单,但是处理用户在警报框中输入数据更为复杂。下面的代码将创建一个带有文本字段的警报框,以便用户可以输入一些文本。
// 创建警报框
let alertController = UIAlertController(
title: "输入一些文本",
message: nil,
preferredStyle: .alert)
// 添加文本框
alertController.addTextField(
configurationHandler: {(textField: UITextField!) -> Void in
textField.placeholder = "输入一些文本"
})
// 添加确定按钮
let okAction = UIAlertAction(
title: "确定",
style: .default,
handler: {(alert: UIAlertAction!) -> Void in
let textField = alertController.textFields![0] as UITextField
print("输入的文本是:\(textField.text!)")
})
alertController.addAction(okAction)
// 显示警报框
self.present(alertController, animated: true, completion: nil)
在上面的代码中,我们首先创建一个带有标题“输入一些文本”的UIAlertController
。然后,我们使用addTextField(configurationHandler:)
方法向UIAlertController
中添加文本字段。这个方法需要一个闭包作为参数,这个闭包接受一个UITextField
对象并用它来设置文本字段的一些参数。在本例中,我们设置了文本字段的占位符文本。
接下来,我们使用addAction(_ :)
方法将一个名为“确定”的操作添加到UIAlertController
中。这个操作具有一个处理程序,当用户点击确定按钮时,将调用处理程序并执行相应的操作。我们首先获取文本字段,并将其文本输出到控制台,然后关闭警报框。最后,我们使用present(_:animated:completion:)
方法显示UIAlertController
。
四、自定义行为
如果您需要更高级的行为,例如添加文本字段、覆盖默认方法或创建动态视图,则可以使用UIAlertController
的子类。下面的代码将展示如何创建UIAlertController
子类,并添加一个自定义视图。
// 创建自定义警报框
class CustomAlertController: UIAlertController {
override func viewDidLoad() {
super.viewDidLoad()
// 创建自定义视图
let customView = UIView(frame: CGRect(x:0, y:0, width:270, height:150))
customView.backgroundColor = UIColor.white
// 添加标签
let label = UILabel(frame: CGRect(x:10, y:10, width:250, height:30))
label.text = "这是一个自定义视图"
label.textColor = UIColor.black
label.textAlignment = NSTextAlignment.center
customView.addSubview(label)
// 添加文本框
let textField = UITextField(frame: CGRect(x:10, y:50, width:250, height:30))
textField.placeholder = "请输入一些文本"
textField.borderStyle = UITextBorderStyle.roundedRect
customView.addSubview(textField)
// 添加按钮
let okButton = UIButton(type: .system)
okButton.frame = CGRect(x: 100, y: 100, width: 70, height: 30)
okButton.setTitle("确定", for: UIControl.State.normal)
okButton.addTarget(
self,
action: #selector(CustomAlertController.okButtonTapped(_:)),
for: UIControl.Event.touchUpInside)
customView.addSubview(okButton)
// 设置自定义视图
self.view.addSubview(customView)
}
@objc func okButtonTapped(_ sender: UIButton!) {
let parent = self.presentingViewController
self.dismiss(animated: true, completion: {() -> Void in
let alertController = UIAlertController(
title: "提示",
message: "自定义视图已关闭",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "确定",
style: .default,
handler: nil)
alertController.addAction(okAction)
parent?.present(alertController, animated: true, completion: nil)
})
}
}
// 显示自定义警报框
let customAlertController = CustomAlertController(
title: nil,
message: nil,
preferredStyle: .alert)
self.present(
customAlertController,
animated: true,
completion: {() -> Void in
customAlertController.viewDidLoad()
})
在上面的代码中,我们创建了一个CustomAlertController
类,它是UIAlertController
的子类。我们将为自定义视图创建一个自定义视图,其中包括一个标签、一个文本字段和一个按钮。该类包括一个名为okButtonTapped:
的方法,该方法是“确定”按钮的操作,用于关闭自定义警报框并创建另一个标准警报框。
最后,我们使用自定义警报框CustomAlertController
来替换原来的警报框,将其显示在屏幕上。我们使用UIViewController
的present(_:animated:completion:)
方法来显示自定义警报框,并在显示后调用viewDidLoad
方法做一些额外的设置。