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

Nam*_*tha 295 popup ios

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

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,你可以UIAlertViewinit方法中继承并放入自定义UI组件.如果要在UIAlertView出现后按响应按钮,可以设置delegate上述内容并实施该- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法.

你可能也想看一下UIActionSheet.

  • Apple文档说"UIAlertView类旨在按原样使用,不支持子类化".https://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html (44认同)
  • 只是评论:启用ARC后,不需要'[alert release]'(至少,编译器这样说). (39认同)
  • 从iOS 4开始不支持对UIAlertView进行子类化 (4认同)
  • [这里](http://stackoverflow.com/a/5763609) 是一个带有委托的简单 UIAlertView 的示例,如果您还需要按钮操作 (2认同)
  • 如果您正在寻找一个快速版本,请查看[Oscar Swanros](http://stackoverflow.com/a/24022696/996404)答案 (2认同)

Sur*_*gch 167

通过弹出框提出这个问题的不同人意味着不同的事情.我强烈建议您阅读Temporary Views文档.我的回答主要是对此文档和其他相关文档的总结.

提醒(给我举个例子)

在此输入图像描述

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

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

在此输入图像描述

笔记:

行动表(给我举个例子)

在此输入图像描述

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

重要提示: UIActionSheet在iOS的8已被弃用(请注意,UIActionSheetDelegate也已经过时.)创建和iOS 8的管理动作片,后来,改用UIAlertControllerpreferredStyleUIAlertControllerStyleActionSheet.

模态视图(给我举个例子)

在此输入图像描述

一个模式的看法是,有它需要完成任务的一切自包含的视图.它可能会也可能不会占据整个屏幕.要创建模态视图,请使用UIPresentationController其中一种模态演示样式.

也可以看看

Popover (给我举个例子)

在此输入图像描述

酥料饼的是,当用户点击的东西,接出它消失时出现的视图.它有一个箭头,显示了水龙头的控制位置.内容可以是您可以放在View Controller中的任何内容.你用一个弹出窗口UIPopoverPresentationController.(在iOS 8之前,UIPopoverController是推荐的方法.)

过去只有iPad上有弹出窗口,但从iOS 8开始你也可以在iPhone上看到它们(见这里,这里这里).

也可以看看

通知

在此输入图像描述

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

在此输入图像描述

也可以看看

关于Android Toasts的说明

在此输入图像描述

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

来自Android背景的人想知道Toast的iOS版本是什么.他可以在这里,这里,这里这里找到这些问题的一些例子.答案是在iOS中没有相当于Toast的东西.提出的各种解决方法包括:

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

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

  • 还有关于Android Toast的说明!太好了!此信息可帮助来自Android开发的新开发人员.谢谢! (3认同)
  • 伙计,我需要打印和框架!你昨天和今天再次救了我:D (3认同)

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)


Sur*_*gch 6

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

如何实现弹出对话框

在此输入图像描述

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 次

最近记录:

6 年,9 月 前