如何在iOS中实现弹出对话框

时间:2011-02-14 03:20:18

标签: ios popup

计算完毕后,我想显示一个向用户传达信息的弹出框或警告框。有谁知道我可以在哪里找到更多相关信息?

7 个答案:

答案 0 :(得分:515)

是的,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中显示自定义用户界面,您可以继承UIAlertView并在init方法中添加自定义用户界面组件。如果您想在显示UIAlertView后按下按钮,可以设置上面的delegate并实施- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法。

您可能还想查看UIActionSheet

答案 1 :(得分:162)

不同的人通过弹出框来表达不同的东西。我强烈建议您阅读Temporary Views文档。我的回答主要是对此文档和其他相关文档的总结。

提醒(show me an example)

enter image description here

Alerts显示标题和可选消息。在继续之前,用户必须确认它(一键式警报)或做出简单的选择(双键警报)。您使用UIAlertController创建提醒。

值得引用文档的警告和有关创建不必要警报的建议。

enter image description here

注意:

行动表(show me an example)

enter image description here

Action Sheets为用户提供一系列选择。它们出现在屏幕底部或弹出窗口中,具体取决于设备的大小和方向。与警报一样,UIAlertController用于制作操作表。在iOS 8之前,使用了UIActionSheet,但现在documentation说:

  

重要提示: UIActionSheet在iOS 8中已弃用。(请注意,UIActionSheetDelegate也已弃用。)要在iOS 8及更高版本中创建和管理操作表,请改用UIAlertController preferredStyle UIAlertControllerStyleActionSheet (show me an example)

模态视图enter image description here

modal view

UIPresentationController是一个自包含的视图,包含完成任务所需的一切。它可能会也可能不会占据整个屏幕。要创建模态视图,请使用Modal Presentation StylesPresenting View Controllers from Other View Controllers之一。

另见

Popover enter image description here

Popover

UIPopoverPresentationController是当用户点按某些内容时显示的视图,并在点击它时消失。它有一个箭头,显示了水龙头的控制位置。内容可以是您可以放在View Controller中的任何内容。你使用UIPopoverController制作一个弹出窗口。 (在iOS 8之前,here是推荐的方法。)

过去只有iPad上有弹出窗口,但从iOS 8开始,你也可以在iPhone上看到它们(见herehereView Controllers: Popovers)。< / p>

另见

  • enter image description here

通知

Notifications

enter image description here是声音/振动,警报/横幅或徽章,即使应用未在前台运行,也会通知用户某些内容。

Local and Remote Notification Programming Guide

另见

关于Android Toasts的说明

Toast

在Android中,here是短消息,会在屏幕上显示很短的时间,然后自动消失,而不会中断用户与应用的互动。

来自Android背景的人想知道Toast的iOS版本是什么。他可以找到这些问题的一些示例hereherehere和{{3}}。答案是没有相当于iOS中的Toast 。提出的各种解决方法包括:

  • 使用子类UIView
  • 创建自己的
  • 导入模仿Toast的第三方项目
  • 使用带有计时器的无按钮警报

但是,我的建议是坚持iOS已经提供的标准UI选项。不要试图使您的应用看起来和行为与Android版本完全相同。考虑如何重新打包它,使其看起来和感觉像一个iOS应用程序。

答案 2 :(得分:57)

自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: UIAlertController.Style.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertAction.Style.default)
        {
            (UIAlertAction) -> Void in
        }
        alert.addAction(alertAction)
        present(alert, animated: true)
        {
            () -> Void in
        }

答案 3 :(得分: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];
}

我的示例中的self是一个UIViewController,它为弹出窗口实现“presentViewController”方法。

大卫

答案 4 :(得分:10)

对于Swift 3&amp;斯威夫特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)

已弃用:

这是受检查回复启发的快速版本:

显示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) {


}

答案 5 :(得分:6)

虽然我已经编写了overview种不同类型的弹出窗口,但大多数人只需要一个警报。

如何实现弹出对话框

enter image description here

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

答案 6 :(得分:0)

这是Xamarin.iOS中的C#版本

var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok");
alert.Show();
相关问题