欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-30 08:34:56
...

本文翻译自: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? 有谁知道在哪里可以找到更多相关信息?


#1楼

参考:https://stackoom.com/question/Kvki/如何在iOS中实现弹出对话框


#2楼

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: 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楼

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

Alert (show me an example) 提醒(给我举个例子)

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

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. 值得引用文档的警告和有关创建不必要警报的建议。

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

Notes: 笔记:

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

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

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的管理动作片,后来,改用UIAlertControllerpreferredStyleUIAlertControllerStyleActionSheet

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

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

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 . 要创建模态视图,请使用UIPresentationController和其中一个模态演示样式

See also 也可以看看

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

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

A Popover is 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. 内容可以是您可以放在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 ). 过去只有iPad上有弹出窗口,但从iOS 8开始你也可以在iPhone上看到它们(见这里这里这里 )。

See also 也可以看看

Notifications 通知

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

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

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

See also 也可以看看

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

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

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背景的人想知道Toast的iOS版本是什么。 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自己的
  • 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应用程序。


#4楼

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 大卫


#5楼

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 wants here
        })
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)

Deprecated : 不推荐:

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

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


}

#6楼

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

var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok");
alert.Show();
相关标签: ios popup