【发布时间】:2011-06-26 16:34:44
【问题描述】:
计算后,我想显示一个弹出框或警告框,向用户传达消息。有谁知道我在哪里可以找到有关此的更多信息?
【问题讨论】:
计算后,我想显示一个弹出框或警告框,向用户传达消息。有谁知道我在哪里可以找到有关此的更多信息?
【问题讨论】:
是的,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];
如果你想做一些更花哨的事情,比如在 UIAlertView 中显示自定义 UI,你可以继承 UIAlertView 并在 init 方法中放入自定义 UI 组件。如果要在出现UIAlertView 后响应按钮按下,可以设置上面的delegate,并实现- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 方法。
您可能还想查看UIActionSheet。
【讨论】:
自 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
}
如您所见,API 允许我们为操作和显示警报实现回调,这非常方便!
为 Swift 4.2 更新
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK!", style: .default)
{
(UIAlertAction) -> Void in
}
alert.addAction(alertAction)
present(alert, animated: true)
{
() -> Void in
}
【讨论】:
提出这个问题的不同人通过弹出框表示不同的意思。我强烈推荐阅读Temporary Views 文档。我的回答主要是对本文档和其他相关文档的总结。
Alerts 显示标题和可选消息。用户必须在继续之前确认它(一键警报)或做出简单的选择(两键警报)。您使用UIAlertController 创建警报。
值得引用文档中有关创建不必要警报的警告和建议。
注意事项:
UIAlertView 已弃用。您现在应该使用 UIAlertController 创建提醒。Action Sheets 给用户一个选择列表。它们出现在屏幕底部或弹出窗口中,具体取决于设备的大小和方向。与警报一样,UIAlertController 用于制作操作表。在 iOS 8 之前,使用 UIActionSheet,但现在 documentation 表示:
重要提示:
UIActionSheet在 iOS 8 中已弃用。(请注意,UIActionSheetDelegate也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请改用UIAlertController和preferredStyle的UIAlertControllerStyleActionSheet。
modal view 是一个独立的视图,包含完成任务所需的一切。它可能会也可能不会占据全屏。要创建模态视图,请使用 UIPresentationController 和 Modal Presentation Styles 之一。
另见
Popover 是一个视图,当用户点击某物时出现,点击它时消失。它有一个箭头,显示进行点击的控件或位置。内容可以是您可以放入视图控制器中的任何内容。您使用UIPopoverPresentationController 制作弹出框。 (在 iOS 8 之前,UIPopoverController 是推荐的方法。)
过去弹出框只能在 iPad 上使用,但从 iOS 8 开始,您也可以在 iPhone 上使用它们(请参阅here、here 和 here)。
另见
Notifications 是声音/振动、警报/横幅或徽章,即使应用程序未在前台运行,也会通知用户某事。
另见
在 Android 中,Toast 是一条短消息,会在屏幕上显示一小段时间,然后自动消失,而不会中断用户与应用的交互。
具有 Android 背景的人想知道 iOS 版本的 Toast 是什么。他可以找到这些问题的一些示例here、here、here 和here。答案是在 iOS 中没有与 Toast 等效的功能。已提出的各种解决方法包括:
UIView 自己制作
但是,我的建议是坚持使用 iOS 已经提供的标准 UI 选项。不要试图让您的应用程序的外观和行为与 Android 版本完全相同。想想如何重新打包它,让它看起来和感觉都像一个 iOS 应用程序。
【讨论】:
从 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];
}
在我的示例中,self 是一个 UIViewController,它为弹出窗口实现了“presentViewController”方法。
【讨论】:
对于 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 want here
})
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
已弃用:
这是受检查响应启发的 swift 版本:
显示警报视图:
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()
将委托添加到您的视图控制器:
class AgendaViewController: UIViewController, UIAlertViewDelegate
当用户点击按钮时,此代码将被执行:
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
}
【讨论】:
这是 Xamarin.iOS 中的 C# 版本
var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok");
alert.Show();
【讨论】:
虽然我已经写了一个overview 不同类型的弹出窗口,但大多数人只需要一个警报。
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)
}
}
我更完整的答案是here。
【讨论】: