在进行计算后,我想显示一个弹出框或警示框向用户传达信息。请问有人知道我可以在哪里找到更多相关信息吗?
没错,你可能正在寻找一个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
。
警报框 显示标题和可选消息。在继续之前,用户必须确认它(一个单按钮警报)或做出简单的选择(一个双按钮警报)。您可以使用 UIAlertController
创建警报。
值得引用文档关于创建不必要的警报的警告和建议。
注意:
UIAlertView
已被弃用。现在应该使用 UIAlertController
创建警报。操作表(Action Sheets)显示给用户一个选项列表,它们会出现在屏幕底部或者弹出视图中,具体取决于设备的尺寸和方向。与警告一样,使用 UIAlertController
来创建操作表。在 iOS 8 之前,使用 UIActionSheet
,但是现在文档中指明:
重要提示:
UIActionSheet
在 iOS 8 中已被废弃(注意,UIActionSheetDelegate
也已被废弃)。在 iOS 8 或更高版本中,若要创建和管理操作表,请改用带有preferredStyle
属性设置为UIAlertControllerStyleActionSheet
的UIAlertController
。
模态视图是一个自包含的视图,它拥有完成任务所需的一切内容。它可以占据整个屏幕,也可以不是。要创建模态视图,请使用带有一个 模态呈现样式(Modal Presentation Styles) 的 UIPresentationController
。
另请参见
一个气泡窗口是一个视图,当用户点击某个内容时出现并在取消点击后消失。 它有一个箭头,显示控件或位置来自于哪里进行了点击。其内容可以是任何您可以放置在视图控制器中的内容。您可以使用UIPopoverPresentationController
创建气泡窗口 (在iOS 8之前,建议使用UIPopoverController
方法)。
过去气泡窗口只在iPad上可用,但从iOS 8开始,您也可以在iPhone上获取它们(请参见这里,这里和这里)。
还请参考
通知是指声音/震动、警报/横幅或标记,即使应用程序不在前台运行也会通知用户某些信息。
参见
在Android中,Toast是一条短消息,会在屏幕上显示一段时间,然后自动消失,不会干扰用户与应用程序的交互。
来自Android背景的人想知道iOS版本的Toast是什么。这些问题的一些例子可以在这里、这里、这里和这里找到。答案是:iOS中没有Toast的等效物。已经提出了各种解决方法,包括:
UIView
制作自己的Toast但是,我的建议是坚持使用已经附带于iOS的标准UI选项。不要尝试使您的应用程序与Android版本完全相同。考虑如何重新打包它,以使其看起来和感觉像iOS应用程序。
自从iOS 8发布以来,UIAlertView
已被废弃;UIAlertController 是替代品。
这是在Swift 5中的示例:
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK!", style: .default) { (sender: UIAlertAction) -> Void in
// ... Maybe handle "OK!" being tapped.
}
alert.addAction(alertAction)
// Show.
present(alert, animated: true) { () -> Void in
// ... Maybe do something once showing is complete.
}
正如您所见,API允许我们为操作和呈现警报时实现回调函数,这非常方便!
对于旧版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
}
自从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 上显示警告框的好方法。
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 版本:
显示 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()
将代理添加到您的视图控制器中:
class AgendaViewController: UIViewController, UIAlertViewDelegate
当用户点击按钮时,将执行此代码:
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
}
虽然我已经写了一个关于不同类型的弹出框的概述,但大多数人只需要一个警告框。
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)
}
}
我的详细答案在这里。
我更加完整的回答可以在这里找到。这是Xamarin.iOS中的C#版本
var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok");
alert.Show();