使用警报控制器的iOS操作表

时间:2020-02-23 14:45:52  来源:igfitidea点击:

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

iOS操作表

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

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

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

  • 默认值:正常选项。
    默认颜色为蓝色。

  • " destructive":默认颜色为红色,通常用于"删除"选项。

  • 取消:位于ActionSheet的底部,用于关闭它。

在Swift中创建一个ActionSheet:

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

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

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

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

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

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

iOS操作表示例情节提要

默认情况下," Main.Storyboard"为空。
我们将为每种ActionSheets类型添加三个按钮。
还要设置自动布局约束。
以下是我们的Main.storyboard的外观:

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

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

import UIKit

class 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 in
          print("Action 1")
      })
      
      let action2 = UIAlertAction(title: "Option 2", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("Action 2")
      })
      
      
      let maybeAction = UIAlertAction(title: "Maybe", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("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 in
          print("delete")
      })
      
      let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (alert: UIAlertAction!) -> Void in
          print("Cancel")
      })
      
      let noAction = UIAlertAction(title: "No", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("No")
      })
      
      let maybeAction = UIAlertAction(title: "Maybe", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("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 San Franceco ever win a FIFA World Cup?", preferredStyle: .actionSheet)
      
      let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("Yes")
      })
      
      let noAction = UIAlertAction(title: "No", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("No")
      })
      
      let maybeAction = UIAlertAction(title: "Maybe", style: .default, handler: { (alert: UIAlertAction!) -> Void in
          print("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.
  }

}

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

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

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

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

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