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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4988564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:53:08  来源:igfitidea点击:

how to implement a pop up dialog box in iOS

iospopup

提问by Namratha

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?

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

回答by donkim

Yup, a UIAlertViewis probably what you're looking for. Here's an example:

UIAlertView是的, a可能就是你要找的。下面是一个例子:

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 UIAlertViewand put in custom UI components in the initmethod. If you want to respond to a button press after a UIAlertViewappears, you can set the delegateabove and implement the - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndexmethod.

如果你想做一些更奇特的事情,比如在你的 中显示自定义 UI UIAlertView,你可以子类化UIAlertView并在init方法中放入自定义 UI 组件。如果你想在 aUIAlertView出现后响应一个按钮按下,你可以设置delegate上面的并实现该- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法。

You might also want to look at the UIActionSheet.

您可能还想查看UIActionSheet.

回答by Suragch

Different people who come to this question mean different things by a popup box. I highly recommend reading the Temporary Viewsdocumentation. My answer is largely a summary of this and other related documentation.

来到这个问题的不同人通过弹出框表示不同的意思。我强烈建议阅读临时视图文档。我的回答主要是对本文档和其他相关文档的总结。

Alert (show me an example)

警报(给我看一个例子)

enter image description here

在此处输入图片说明

Alertsdisplay 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.

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

enter image description here

在此处输入图片说明

Notes:

笔记:

Action Sheet (show me an example)

行动表(给我看一个例子)

enter image description here

在此处输入图片说明

Action Sheetsgive 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 UIAlertControlleris used to make an action sheet. Before iOS 8, UIActionSheetwas used, but now the documentationsays:

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

Important:UIActionSheetis deprecated in iOS 8. (Note that UIActionSheetDelegateis also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertControllerwith a preferredStyleof UIAlertControllerStyleActionSheet.

重要提示:UIActionSheet在 iOS 8 中UIActionSheetDelegate已弃用。(请注意,它也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请使用UIAlertControllera preferredStyleof UIAlertControllerStyleActionSheet

Modal View (show me an example)

模态视图(给我看一个例子)

enter image description here

在此处输入图片说明

A modal viewis 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 UIPresentationControllerwith one of the Modal Presentation Styles.

一个模式的看法是,有它需要完成任务的一切自包含的视图。它可能会也可能不会占据全屏。要创建模态视图,UIPresentationController请将a与Modal Presentation Styles之一结合使用。

See also

也可以看看

Popover (show me an example)

Popover (给我看一个例子)

enter image description here

在此处输入图片说明

A Popoveris a view that appears when a user taps on something and disappears when tapping off it. 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. (Before iOS 8, UIPopoverControllerwas the recommended method.)

酥料饼是接出它的时候出现,当用户点击的东西,消失的景色。它有一个箭头,显示点击的控件或位置。内容可以是您可以放入视图控制器的任何内容。你用一个UIPopoverPresentationController. (在 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

通知

enter image description here

在此处输入图片说明

Notificationsare sounds/vibrations, alerts/banners, or badges that notify the user of something even when the app is not running in the foreground.

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

enter image description here

在此处输入图片说明

See also

也可以看看

A note about Android Toasts

关于 Android Toasts 的说明

enter image description here

在此处输入图片说明

In Android, a Toastis 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. 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. Various workarounds that have been presented include:

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

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

However, my advice is to stick with the standard UI options that already come with iOS. Don't try to make your app look and behave exactly the same as the Android version. Think about how to repackage it so that it looks and feels like an iOS app.

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

回答by Entalpi

Since the release of iOS 8, UIAlertViewis now deprecated; UIAlertControlleris the replacement.

自 iOS 8 发布以来,UIAlertView现已弃用;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: UIAlertController.Style.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertAction.Style.default)
        {
            (UIAlertAction) -> Void in
        }
        alert.addAction(alertAction)
        present(alert, animated: true)
        {
            () -> Void in
        }

回答by us_david

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

大卫

回答by Kevin ABRIOUX

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


}

回答by Suragch

Although I already wrote an overviewof different kinds of popups, most people just need an Alert.

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

How to implement a popup dialog box

如何实现弹出对话框

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

My fuller answer is here.

我更完整的答案在这里

回答by Art Drozdov

Here is C# version in Xamarin.iOS

这是 Xamarin.iOS 中的 C# 版本

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