- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
找不到对此的清晰且信息丰富的解释。
最佳答案
在某个主题上搜索了一段时间后,我没有找到一个明确的解释,即使在它的类引用中 UIAlertController Reference
还可以,但对我来说还不够清晰。
所以在收集了一些和平之后我决定做出自己的解释(希望对你有帮助)
就是这样:
UIAlertView
如指出的那样已弃用: UIAlertView in Swift UIAlertController
应该在 iOS8+ 中使用所以要首先创建一个我们需要实例化它,Constructor(init) 有 3 个参数:2.1 title:String -> 显示在警告对话框顶部的粗体文本
2.2 message:String -> smaller text (几乎解释了它自己)
2.3 prefferedStyle:UIAlertControllerStyle
-> 定义对话框样式,大多数情况下:UIAlertControllerStyle.Alert
现在要实际向用户展示它,我们可以使用 showViewController
或 presentViewController
并将我们的警报作为参数传递
要添加一些与用户的交互,我们可以使用:
4.1 UIAlertController.addAction
创建按钮
4.2UIAlertController.addTextField
创建文本字段
编辑注释:下面的代码示例,针对 swift 3 语法进行了更新
示例 1:简单对话框
@IBAction func alert1(sender: UIButton) {
//simple alert dialog
let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);
//show it
show(alert, sender: self);
}
示例 2:具有一个输入文本字段和两个按钮的对话框
@IBAction func alert2(sender: UIButton) {
//Dialog with one input textField & two buttons
let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);
//default input textField (no configuration...)
alert.addTextField(configurationHandler: nil);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
let fields = alert.textFields!;
print("Yes we can: "+fields[0].text!);
}));
present(alert, animated: true, completion: nil);
}
示例3:一个自定义输入文本框和一个按钮
@IBAction func alert3(sender: UIButton) {
// one input & one button
let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);
//configured input textField
var field:UITextField?;// operator ? because it's been initialized later
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder="I am displayed, when there is no value ;-)";
input.clearButtonMode=UITextFieldViewMode.whileEditing;
field=input;//assign to outside variable(for later reference)
});
//alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later
func yesHandler(actionTarget: UIAlertAction){
print("YES -> !!");
//print text from 'field' which refer to relevant input now
print(field!.text!);//operator ! because it's Optional here
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));
present(alert, animated: true, completion: nil);
}
希望对您有所帮助,祝您好运 ;-)
关于ios - 在哪里可以找到有关 swift alert (UIAlertController) 的明确解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276503/
我们如何从第一个警报打开第二个 UIAlert? 第一段代码工作正常,显示了警报。但是我希望能够调用第二个警报 View ,如果选择了第一个警报中的一个选项,就会出现该 View 。 在下面的示例中,
我有一个 Parse Sign Up 和一个 UIAlertController,我希望 UIAlertController 显示一个指示器,但在注册出现错误时被另一个 UIAlertControll
如何在 UIAlertController 外部点击时关闭 UIAlertController? 我可以添加 UIAlertActionStyleCancel 样式的 UIAlertAction 来关
这是我的代码:请检查是否出了什么问题?请给我 UIActionsheet (UIAlertController) 的代码,其中包含 swift 中的数组 let alertA = UIAlertCon
我正在使用 UIAlertController 要求用户输入一个字符串,这个字符串不能为空。为此,我为位于 UIAlertController 上的 UITextField 上的每个编辑事件添加了处理
我想介绍一个 UIAlertController与 UIAlertControllerStyleAlert风格,同时解雇 UIAlertController与 UIAlertControllerSty
我正在使用 UIAlertController 获取用户输入并更新表格单元格。每次当我尝试创建警报时,我都会在控制台中收到以下警告 2015-11-19 17:51:42.034 SimpleTabl
我正在尝试将 UIAlertController 中的推送通知(PN)中的消息从 AppDelegate 呈现到当前 ViewController。如果我发送 1 个 PN,没有问题,并且会显示警报!
我正在尝试从 UIAlertController 文本字段中获取文本。有人可以告诉我我做错了什么,因为它不起作用。我得到一个零返回。 - (IBAction)btnMakeRec {
我正在将我的应用程序移植到 iOS 8.0 并注意到 UIAlertView 已被弃用。 所以我改变了使用 UIAlertController 的方法。这在大多数情况下都有效。 除了,当我的应用程序打
我想根据代码更改警报操作按钮的顺序,我尝试了所有可能性,但不允许我更改顺序。 根据 HIG,取消在右侧 https://developer.apple.com/library/ios/document
在 iOS 8 及更低版本中显示 UIActionSheet当键盘出现时,将在键盘上显示操作表。在 iOS 9 中,情况不再如此。 在我的应用程序中,我们有一个聊天功能,并希望通过键盘显示一个 Act
嗨,我想知道是否可以使用UIAlert播放声音?根据以下文章,这似乎是可能的,但我正在努力将Obj-C转换为Swift。任何帮助表示赞赏! IOS alert message with sound 最
我需要一个系统来隐藏所有UIAlertController当她进入后台时在我的应用程序中。目前我使用 BaseViewController我所有的类(class)UIViewControllers注册
我正在调用带有2个文本字段的UIAlertController(在Obj-C中)。我已经定义了需要在警报中使用的全局UITextField,因为我需要利用UITextFieldDelegate。 我的
我在 View 中添加了一个 UIAlterController。 IT 触发很好,看起来很好,并且有它的 Action ,但是点击取消 Action 什么都不做,它不运行任何代码,即使我添加一个 b
这个问题在这里已经有了答案: How to add textField in UIAlertController? (4 个回答) 4年前关闭。 我可以使用在 UIAlertController 中添
我正在开发一个带有深色主题的 iOS 应用程序,我想使用 UIAlertController .但是,白色警报看起来不合适。即使在黑暗主题的应用程序上使用黑暗主题是否违反人机界面指南 UIAlertC
我正在做一个使用 UIAlertView 的项目,现在的问题是我必须替换所有 UIAlertView's与 UIAlertController's代码中有大约 1250 个。我打算使用现有的 Util
我正在尝试使用操作表样式创建 UIAlertController,但我想将背景颜色更改为灰色。我已经能够找到一种方法来更改 UIAlertController 的背景颜色,但不能更改取消按钮。单独的取
我是一名优秀的程序员,十分优秀!