失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 对警报线程池的警报线程_使用警报控制器的iOS操作表

对警报线程池的警报线程_使用警报控制器的iOS操作表

时间:2021-08-03 21:15:57

相关推荐

对警报线程池的警报线程_使用警报控制器的iOS操作表

对警报线程池的警报线程

In this tutorial, we’ll be implementing Action Sheet in our iOS Application using Swift.

在本教程中,我们将使用Swift在iOS应用程序中实现操作表。

iOS操作表 (iOS Action Sheet)

UIActionSheetclass has now been deprecated. In order to implement ActionSheets we useUIAlertControllernow.

UIActionSheet类现在已被弃用。 为了实现ActionSheets,我们现在使用UIAlertController

ActionSheets are like BottomSheet Dialogs in Android. They slide up from the bottom of the screen. You must select one of the options present in it. An ActionSheet doesn’t close otherwise.

ActionSheets就像Android中的BottomSheet对话框一样。 它们从屏幕底部向上滑动。 您必须选择其中的选项之一。 否则,ActionSheet不会关闭。

An ActionSheet can have buttons of three styles:

ActionSheet可以具有三种样式的按钮:

default: The normal options. Default color is blue.default:正常选项。 默认颜色为蓝色。destructive: Default color is red and is generally used for Delete options.destructive:默认颜色为红色,通常用于“删除”选项。cancel: This comes at the bottom of the ActionSheet and is used to close it.cancel:它位于ActionSheet的底部,用于关闭它。

Creating an ActionSheet in Swift:

在Swift中创建一个ActionSheet:

let alertController = UIAlertController(title: "Question", message: "Will India ever win a FIFA World Cup?", preferredStyle: .actionSheet)let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Yes")})alertController.addAction(yesAction)self.present(alertController, animated: true, completion: nil)

We’ve usedUIAlertControllerwith the preferred style set toactionSheet. The default style isalertwhich is used for displaying alert dialogs.

我们使用UIAlertController并将首选样式设置为actionSheet。 默认样式是alert,用于显示警报对话框。

addActionis used to add a UIAlertAction option into the ActionSheet.

addAction用于将UIAlertAction选项添加到ActionSheet中。

In the UIAlertAction, we define the style as default in the above code. We could use destructive or cancel as well which we will do in the XCode project below.

在UIAlertAction中,我们在上述代码中将样式定义为默认样式。 我们也可以使用破坏性或取消性,这将在下面的XCode项目中进行。

presentdisplays the ActionSheet instance in our View.

present在我们的视图中显示ActionSheet实例。

Let’s create a new XCode project. We will create a single view application.

让我们创建一个新的XCode项目。 我们将创建一个单视图应用程序。

iOS操作表示例情节提要 (iOS Action Sheet Example Storyboard)

TheMain.Storyboardis empty by default.

We’ll add three Buttons for each of the ActionSheets types. Set the Auto-layout constraints as well.

Following is how our Main.storyboard looks :

默认情况下,Main.Storyboard为空。

我们将为每种ActionSheets类型添加三个Button。 还要设置自动布局约束。

以下是我们的Main.storyboard的外观:

Each of the Buttons IBActions is linked to the ViewController.swift using the assistant editor.

使用助手编辑器将每个Button IBAction链接到ViewController.swift。

The code for the ViewController.swift is given below.

下面给出了ViewController.swift的代码。

import UIKitclass ViewController: UIViewController {@IBAction func showColored(_ sender: Any) {let alertController = UIAlertController(title: nil, message: "Colored options exists", preferredStyle: .actionSheet)let action1 = UIAlertAction(title: "Option 1", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Action 1")})let action2 = UIAlertAction(title: "Option 2", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Action 2")})let maybeAction = UIAlertAction(title: "Maybe", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Maybe")})action1.setValue(UIColor.purple, forKey: "titleTextColor")action2.setValue(UIColor.green, forKey: "titleTextColor")maybeAction.setValue(UIColor.black, forKey: "titleTextColor")alertController.addAction(action1)alertController.addAction(action2)alertController.addAction(maybeAction)self.present(alertController, animated: true, completion: nil)}@IBAction func showDeleteAndCancel(_ sender: Any) {let alertController = UIAlertController(title: "Title", message: "Delete And Cancel option exists", preferredStyle: .actionSheet)let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (alert: UIAlertAction!) -> Void inprint("delete")})let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (alert: UIAlertAction!) -> Void inprint("Cancel")})let noAction = UIAlertAction(title: "No", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("No")})let maybeAction = UIAlertAction(title: "Maybe", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Maybe")})alertController.addAction(deleteAction)alertController.addAction(cancelAction)alertController.addAction(noAction)alertController.addAction(maybeAction)self.present(alertController, animated: true, completion: nil)}@IBAction func showNormal(_ sender: Any) {let alertController = UIAlertController(title: "Question", message: "Will India ever win a FIFA World Cup?", preferredStyle: .actionSheet)let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Yes")})let noAction = UIAlertAction(title: "No", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("No")})let maybeAction = UIAlertAction(title: "Maybe", style: .default, handler: { (alert: UIAlertAction!) -> Void inprint("Maybe")})alertController.addAction(yesAction)alertController.addAction(noAction)alertController.addAction(maybeAction)self.present(alertController, animated: true, completion: nil)}@IBOutlet weak var showNormal: UIButton!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

showNormalfunction displays an ActionSheet with buttons of the default style.

showNormal函数显示带有默认样式的按钮的ActionSheet。

To change the text color of the options in the ActionSheet from the default color blue, we use the setValue method and pass the UIColor for the standard key of the text color:

要将ActionSheet中选项的文本颜色从默认颜色蓝色更改,我们使用setValue方法并将UIColor传递给文本颜色的标准键:

action1.setValue(UIColor.purple, forKey: "titleTextColor")

On every UIAlertAction we have a handler set which is basically a Swift closure. Inside the closure, we can add the logic for the action. Here we’ve just printed the name of the action.

在每个UIAlertAction上,我们都有一个处理程序集,该处理程序集基本上是一个Swift闭包。 在闭包内部,我们可以添加操作的逻辑。 在这里,我们刚刚打印了动作的名称。

showDeleteAndCancelfunction displays the ActionSheet with delete and cancel buttons.

showDeleteAndCancel函数显示带有删除和取消按钮的ActionSheet。

The output of the above application in action is given below:

上面应用程序的输出如下:

This brings an end to this tutorial. You can download the XCode iOSActionSheets Project from the link below:

本教程到此结束。 您可以从以下链接下载XCode iOSActionSheets项目:

iOSActionSheetsiOSActionSheets

翻译自: /21985/ios-action-sheet-alert-controller

对警报线程池的警报线程

如果觉得《对警报线程池的警报线程_使用警报控制器的iOS操作表》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。