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

vi4fp9gy  于 2022-10-31  发布在  Swift
关注(0)|答案(4)|浏览(157)

我想创建一个可以在代码中多次使用的操作表。要做到这一点,我需要能够根据操作表标题使用函数。有没有办法将函数作为参数数组(如“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)
  }
}
eimct9ow

eimct9ow1#

对于UIAlert,您只需要更改preferredStyle .alert它,它就可以为UIAlert工作了,下面的代码只需要复制并粘贴它就可以为UIActionSheet工作了。

extension UIViewController {
       func popupAlert(title: String?, message: String?, actionTitles:[String?], actionStyle:[UIAlertAction.Style], actions:[((UIAlertAction) -> Void)?]) {
              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)
              }
              self.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
 }])

如果你有任何疑问,请评论我.谢谢

bybem2ql

bybem2ql2#

我已经找出了最好的方法来添加一个行动表与取消和尽可能多的行动所需的。
创建具有类型别名的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 titleAndAction {
  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() })
      ])
  }
}
pgvzfuti

pgvzfuti3#

您可以传递闭包并在处理程序中调用它,类似这样的操作应该可以正常工作。
也不知道为什么你要传递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

col17t5w

col17t5w4#

据我所知,当警报标题更改时,您需要调用特定函数,并且您希望能够从不同的viewController中执行此操作,我希望这会有所帮助

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

相关问题