don*_*kim 516
是啊,一个UIAlertView
可能是你在找什么.这是一个例子:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
message:@"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
Run Code Online (Sandbox Code Playgroud)
如果你想做一些更加花哨的事情,比如在你的显示器中显示一个自定义UI UIAlertView
,你可以UIAlertView
在init
方法中继承并放入自定义UI组件.如果要在UIAlertView
出现后按响应按钮,可以设置delegate
上述内容并实施该- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
方法.
你可能也想看一下UIActionSheet
.
Sur*_*gch 167
通过弹出框提出这个问题的不同人意味着不同的事情.我强烈建议您阅读Temporary Views文档.我的回答主要是对此文档和其他相关文档的总结.
警报显示标题和可选消息.在继续之前,用户必须确认它(一键式警报)或做出简单的选择(双键警报).您使用a创建警报UIAlertController
.
值得引用文档的警告和有关创建不必要警报的建议.
笔记:
UIAlertView
不推荐在iOS 8 中启动.您应该使用UIAlertController
现在创建警报.操作表为用户提供了一个选项列表.它们出现在屏幕底部或弹出窗口中,具体取决于设备的大小和方向.与警报一样,a UIAlertController
用于制作操作表.在使用iOS 8之前UIActionSheet
,现在文档说:
重要提示:
UIActionSheet
在iOS的8已被弃用(请注意,UIActionSheetDelegate
也已经过时.)创建和iOS 8的管理动作片,后来,改用UIAlertController
了preferredStyle
的UIAlertControllerStyleActionSheet
.
一个模式的看法是,有它需要完成任务的一切自包含的视图.它可能会也可能不会占据整个屏幕.要创建模态视图,请使用UIPresentationController
其中一种模态演示样式.
也可以看看
一酥料饼的是,当用户点击的东西,接出它消失时出现的视图.它有一个箭头,显示了水龙头的控制位置.内容可以是您可以放在View Controller中的任何内容.你用一个弹出窗口UIPopoverPresentationController
.(在iOS 8之前,UIPopoverController
是推荐的方法.)
过去只有iPad上有弹出窗口,但从iOS 8开始你也可以在iPhone上看到它们(见这里,这里和这里).
也可以看看
通知是声音/振动,警报/横幅或徽章,即使应用程序未在前台运行,也会通知用户某些内容.
也可以看看
在Android中,Toast是一条短消息,会在屏幕上显示很短的时间,然后自动消失,而不会中断用户与应用的互动.
来自Android背景的人想知道Toast的iOS版本是什么.他可以在这里,这里,这里和这里找到这些问题的一些例子.答案是在iOS中没有相当于Toast的东西.提出的各种解决方法包括:
UIView
但是,我的建议是坚持iOS已经提供的标准UI选项.不要试图使您的应用看起来和行为与Android版本完全相同.想想如何重新打包它,使它看起来和感觉像一个iOS应用程序.
Ent*_*lpi 58
自iOS 8发布以来,UIAlertView
现已弃用; UIAlertController是替代品.
以下是Swift中的外观示例:
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertActionStyle.default)
{
(UIAlertAction) -> Void in
}
alert.addAction(alertAction)
present(alert, animated: true)
{
() -> Void in
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,API允许我们为操作和当我们提供警报时实现回调,这非常方便!
针对Swift 4.2进行了更新
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertController.Style.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertAction.Style.default)
{
(UIAlertAction) -> Void in
}
alert.addAction(alertAction)
present(alert, animated: true)
{
() -> Void in
}
Run Code Online (Sandbox Code Playgroud)
us_*_*vid 24
针对iOS 8.0进行了更新
从iOS 8.0开始,您将需要使用UIAlertController,如下所示:
-(void)alertMessage:(NSString*)message
{
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Alert"
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction
actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
我的示例中的self是一个UIViewController,它为弹出窗口实现"presentViewController"方法.
大卫
Kev*_*OUX 10
对于Swift 3和Swift 4:
由于UIAlertView已被弃用,因此在Swift 3上显示Alert是一种很好的方法
let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert)
let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in
//Do whatever you wants here
})
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
不推荐:
这是受检查响应启发的快速版本:
显示AlertView:
let alert = UIAlertView(title: "No network connection",
message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok")
alert.delegate = self
alert.show()
Run Code Online (Sandbox Code Playgroud)
将委托添加到视图控制器:
class AgendaViewController: UIViewController, UIAlertViewDelegate
Run Code Online (Sandbox Code Playgroud)
当用户点击按钮时,将执行以下代码:
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
}
Run Code Online (Sandbox Code Playgroud)
虽然我已经写了不同类型弹出窗口的概述,但大多数人只需要一个警报.
class ViewController: UIViewController {
@IBAction func showAlertButtonTapped(_ sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertController.Style.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
我更全面的回答是这里.
归档时间: |
|
查看次数: |
298923 次 |
最近记录: |