如何创建可重用的UIAlert ActionSheet作为UIViewController扩展?

时间:2019-06-13 10:47:06

标签: swift extension-methods uialertcontroller uiactionsheet uialertaction

我想创建一个可以在代码中多次使用的操作表。为此,我需要能够根据操作表标题使用功能。有没有办法像“ title”参数那样将函数作为参数数组传递?

//MARK: - UIAlert action sheet title
enum ActionSheetLabel: String {
  case camera = "Camera"
  case photoLibrary = "Album"
  case cancel = "Cancel"
}



class CameraHandler {
static let cameraHandler = CameraHandler()
func openCamera() { }
func openPhotoLibrary() { }
}


//MARK: - Alert that shows an action sheet with cancel
extension UIViewController {
  func showActionSheetWithCancel(vc: UIViewController, title: [ActionSheetLabel] /*Make a function parameter here to match title*/) {
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

for value in title {
  actionSheet.addAction(UIAlertAction(title: value.rawValue, style: .default, handler: {
    (alert: UIAlertAction!) -> Void in

    //Use the parameter function here to match title

  }))
}

actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel, handler: nil))
vc.present(actionSheet, animated: true, completion: nil)
  }
}

4 个答案:

答案 0 :(得分:0)

您可以传递闭包,并在处理程序中调用它,这样应该可以工作。

也不知道为什么要传递UIViewController,因为您已经在extension UIViewController中定义了该函数,因此我允许我自己删除它并改用self.present

extension UIViewController {
func showActionSheetWithCancel(title: [ActionSheetLabel], action: @escaping () -> ()?) {
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    for value in title {
        actionSheet.addAction(UIAlertAction(title: value.rawValue, style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
           // action
            action()

        }))
    }
    let alertAction = UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel) { (_) in
        action() // or for cancel call it here
    }
    actionSheet.addAction(alertAction)
    self.present(actionSheet, animated: true, completion: nil)


  }
}

您会看到@escaping () -> ()?是可选的,因此您也可以传递nil

答案 1 :(得分:0)

对于UIAlert,您只需要更改preferredStyle。对其进行更改,它便适用于UIAlert,并且在下面的代码中,只需复制并粘贴即可用于UIActionSheet。

  extension UIViewController {
       func popupAlert(title: String?, message: String?, actionTitles:[String?], actionStyle:[UIAlertAction.Style], actions:[((UIAlertAction) -> Void)?], vc: UIViewController) {
              let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
              for (index, title) in actionTitles.enumerated() {
                   let action = UIAlertAction(title: title, style: actionStyle[index], handler: actions[index])
                   alert.addAction(action)
              }
              vc.present(alert, animated: true, completion: nil)
         }
    }

检查以下代码以供使用

self.popupAlert(title: "Alert"), message: “Error in Loading”, actionTitles: ["Okey", "Email"], actionStyle: [.default, .default], actions: [nil,{ action in
         // I have set nil for first button click
         // do your code for second button click
 }], vc: self)

如果您有任何疑问,请发表评论。谢谢

答案 2 :(得分:0)

据我了解,

当警报的标题发生变化时,您需要调用特定的函数,并且您还希望能够通过不同的viewControllers进行更改, 我希望这会有所帮助

extension UIViewController {

 func showActionSheetWithCancel(vc: UIViewController, title: [ActionSheetLabel] ) {
            let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            let cameraHandler = CameraHandler()
            for value in title {

                switch value.rawValue {
                case ActionSheetLabel.camera.rawValue:
                    actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.camera.rawValue, style: .default, handler: { (alert) in
                        cameraHandler.openCamera()

                    }))
                case ActionSheetLabel.photoLibrary.rawValue:
                    actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.photoLibrary.rawValue, style: .default, handler: { (alert) in

                        cameraHandler.openPhotoLibrary()

                    }))
                default:
                    actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel, handler: nil))

                }
                vc.present(actionSheet, animated: true, completion: nil)
            }

        }
}

和该函数的调用将如下所示:

 showActionSheetWithCancel(vc: self, title: [UIViewController.ActionSheetLabel.camera])

答案 3 :(得分:0)

我找到了添加操作表的最佳方法,该操作表带有取消和所需的尽可能多的操作。

使用别名类型创建一个UIViewController扩展:

//MARK: - Alert that shows an action sheet with cancel 
extension UIViewController {
  typealias AlertAction = () -> ()
  typealias AlertButtonAction = (ActionSheetLabel, AlertAction)

  func showActionSheetWithCancel(titleAndAction: [AlertButtonAction]) {
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

for value in title {
  actionSheet.addAction(UIAlertAction(title: value.0.rawValue, style: .default, handler: {
  (alert: UIAlertAction!) -> Void in

    value.1()

}))
}
actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
  }
}

然后,在您要使用它的类或其他地方,以这种方式添加方法:

//MARK: - UIAlert action sheet title
enum ActionSheetLabel: String {
  case camera = "Camera"
  case photoLibrary = "Album"
  case cancel = "Cancel"
}

//MARK: - Class example where to use the action sheet action
class CameraHandler {

fileprivate let currentVC: UIViewController!

func openCamera() { 
// Open user camera
}
func openPhotoLibrary() { 
// Open user photo library
}

  // Method example of this action sheet
  func showActionSheetWithCameraAndLibrary(vc: UIViewController) {

    //This is the way to use the extension
    vc.showActionSheetWithCancel(titleAndAction: [
      (ActionSheetLabel.camera, { [weak self] in self?.openCamera() }),
      (ActionSheetLabel.photoLibrary, { [weak self] in self?.openPhotoLibrary() })
      ])


  }
}