简体   繁体   English

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

[英]how to implement a pop up dialog box in iOS

After a calculation, I want to display a pop up or alert box conveying a message to the user.计算后,我想显示一个向用户传达消息的弹出框或警告框。 Does anyone know where I can find more information about this?有谁知道我在哪里可以找到有关此的更多信息?

Yup, a UIAlertView is probably what you're looking for.是的,一个UIAlertView可能就是你正在寻找的。 Here's an example:下面是一个例子:

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];

If you want to do something more fancy, say display a custom UI in your UIAlertView , you can subclass UIAlertView and put in custom UI components in the init method.如果你想做一些更奇特的事情,比如在你的UIAlertView显示自定义 UI,你可以UIAlertView并在init方法中放入自定义 UI 组件。 If you want to respond to a button press after a UIAlertView appears, you can set the delegate above and implement the - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method.如果你想在UIAlertView出现后响应按钮按下,你可以设置上面的delegate并实现- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法。

You might also want to look at the UIActionSheet .您可能还想查看UIActionSheet

Different people who come to this question mean different things by a popup box.来到这个问题的不同人通过弹出框表示不同的意思。 I highly recommend reading the Temporary Views documentation.我强烈建议阅读临时视图文档。 My answer is largely a summary of this and other related documentation.我的回答主要是对本文档和其他相关文档的总结。

Alert (show me an example)警报(给我看一个例子)

在此处输入图片说明

Alerts display a title and an optional message. 警报显示标题和可选消息。 The user must acknowledge it (a one-button alert) or make a simple choice (a two-button alert) before going on.在继续之前,用户必须确认它(一键式警报)或做出简单的选择(两键式警报)。 You create an alert with a UIAlertController .您使用UIAlertController创建警报。

It is worth quoting the documentation's warning and advice about creating unnecessary alerts.值得引用文档中有关创建不必要警报的警告和建议。

在此处输入图片说明

Notes:注意事项:

Action Sheet (show me an example)行动表(给我看一个例子)

在此处输入图片说明

Action Sheets give the user a list of choices. 操作表为用户提供了一个选项列表。 They appear either at the bottom of the screen or in a popover depending on the size and orientation of the device.它们出现在屏幕底部或弹出窗口中,具体取决于设备的大小和方向。 As with alerts, a UIAlertController is used to make an action sheet.与警报一样, UIAlertController用于制作操作表。 Before iOS 8, UIActionSheet was used, but now the documentation says:在 iOS 8 之前,使用UIActionSheet ,但现在文档说:

Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet .重要提示: UIActionSheet在 iOS 8 中已弃用。(注意UIActionSheetDelegate也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请改用UIAlertControllerpreferredStyle UIAlertControllerStyleActionSheet

Modal View (show me an example)模态视图(给我看一个例子)

在此处输入图片说明

A modal view is a self-contained view that has everything it needs to complete a task. 模态视图是一个独立的视图,它拥有完成任务所需的一切。 It may or may not take up the full screen.它可能会也可能不会占据全屏。 To create a modal view, use a UIPresentationController with one of the Modal Presentation Styles .要创建模态视图,请使用UIPresentationControllerModal Presentation Styles 之一

See also另见

Popover (show me an example) Popover (给我看一个例子)

在此处输入图片说明

A Popover is a view that appears when a user taps on something and disappears when tapping off it. Popover是一种在用户点击某物时出现并在点击时消失的视图。 It has an arrow showing the control or location from where the tap was made.它有一个箭头,显示点击的控件或位置。 The content can be just about anything you can put in a View Controller.内容可以是您可以放入视图控制器的任何内容。 You make a popover with a UIPopoverPresentationController .您使用UIPopoverPresentationController制作一个弹出UIPopoverPresentationController (Before iOS 8, UIPopoverController was the recommended method.) (在 iOS 8 之前,推荐使用UIPopoverController方法。)

In the past popovers were only available on the iPad, but starting with iOS 8 you can also get them on an iPhone (see here , here , and here ).过去,popovers 只能在 iPad 上使用,但从 iOS 8 开始,您也可以在 iPhone 上使用它们(请参阅 此处此处此处)。

See also另见

Notifications通知

在此处输入图片说明

Notifications are sounds/vibrations, alerts/banners, or badges that notify the user of something even when the app is not running in the foreground. 通知是声音/振动、警报/横幅或徽章,即使应用程序未在前台运行,也会通知用户某些事情。

在此处输入图片说明

See also另见

A note about Android Toasts关于 Android Toasts 的说明

在此处输入图片说明

In Android, a Toast is a short message that displays on the screen for a short amount of time and then disappears automatically without disrupting user interaction with the app.在 Android 中, Toast是一条短消息,它会在屏幕上显示一小段时间,然后在不中断用户与应用程序交互的情况下自动消失。

People coming from an Android background want to know what the iOS version of a Toast is.有 Android 背景的人想知道 iOS 版本的 Toast 是什么。 Some examples of these questions can he found here , here , here , and here .他可以在此处此处此处此处找到这些问题的一些示例。 The answer is that there is no equivalent to a Toast in iOS .答案是在 iOS 中没有相当于 Toast 的东西 Various workarounds that have been presented include:已提出的各种解决方法包括:

  • Make your own with a subclassed UIView使用子类UIView您自己的UIView
  • Import a third party project that mimics a Toast导入模仿 Toast 的第三方项目
  • Use a buttonless Alert with a timer使用带计时器的无按钮警报

However, my advice is to stick with the standard UI options that already come with iOS.但是,我的建议是坚持使用 iOS 自带的标准 UI 选项。 Don't try to make your app look and behave exactly the same as the Android version.不要试图让您的应用程序的外观和行为与 Android 版本完全相同。 Think about how to repackage it so that it looks and feels like an iOS app.考虑如何重新打包它,使其看起来和感觉像一个 iOS 应用程序。

Since the release of iOS 8, UIAlertView is now deprecated;自 iOS 8 发布以来, UIAlertView现已弃用; UIAlertController is the replacement. UIAlertController是替代品。

Here is a sample of how it looks in Swift:这是它在 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
}

As you can see, the API allows us to implement callbacks for both the action and when we are presenting the alert, which is quite handy!如您所见,API 允许我们为操作和呈现警报时实现回调,这非常方便!

Updated for Swift 4.2为 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
        }

Updated for iOS 8.0针对 iOS 8.0 更新

Since iOS 8.0, you will need to use UIAlertController as the following:从 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];
}

Where self in my example is a UIViewController, which implements "presentViewController" method for a popup.在我的示例中,self 是一个 UIViewController,它为弹出窗口实现了“presentViewController”方法。

David大卫

For Swift 3 & Swift 4 :对于 Swift 3 和 Swift 4 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3由于 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)

Deprecated :已弃用:

This is the swift version inspired by the checked response :这是受检查响应启发的 swift 版本:

Display 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()

Add the delegate to your view controller :将委托添加到您的视图控制器:

class AgendaViewController: UIViewController, UIAlertViewDelegate

When user click on button, this code will be executed :当用户单击按钮时,将执行此代码:

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {


}

Although I already wrote an overview of different kinds of popups, most people just need an Alert.虽然我已经写了不同类型弹出窗口的概述,但大多数人只需要一个警报。

How to implement a popup dialog box如何实现弹出对话框

在此处输入图片说明

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)
    }
}

My fuller answer is here .我更完整的答案在这里

Here is C# version in Xamarin.iOS这是 Xamarin.iOS 中的 C# 版本

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM