答案 0 :(得分:1)
公共类AlertSheet:NSObject {
public static func showActionSheet(_ actions: [AlertSheetModel], _ title: String,comletion: @escaping (_ action: AlertSheetModel, _ status: Bool) -> Void,onViewCotroller:UIViewController) {
var style = UIAlertController.Style.actionSheet
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) {
style = UIAlertController.Style.alert
}
let alert = UIAlertController(title: nil, message: nil, preferredStyle: style)
for actionData in actions {
let action = UIAlertAction(title: actionData.title
, style: .default, handler: { (UIAlertAction) in
comletion(actionData, true)
})
if let icon = actionData.image {
action.setValue(icon, forKey: "image")
}
action.setValue(true, forKey: "checked")
alert.addAction(action)
}
let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: { (UIAlertAction)in
})
alert.addAction(cancel)
onViewCotroller.present(alert, animated: true, completion: nil)
}
}
打开类AlertSheetModel { 公共变量标题:字符串 公共var键:字符串 公共var图片:UIImage? public var isSelected:布尔=假 公共var对象:可以吗? public init(_ title:String,_ key:String,_ image:UIImage ?, _ isSelected:Bool){ self.title =标题 self.key =键 self.isSelected = isSelected self.image =图片 } }
class testActionSheet:UIViewController {
// initialize the image for following icon
let image1: UIImage = UIImage.init()
let image2: UIImage = UIImage.init()
let image3: UIImage = UIImage.init()
override func viewDidLoad() {
super.viewDidLoad()
self.showActionSheet()
}
private func showActionSheet() {
let actionSheet1 = AlertSheetModel.init("iPhone", "iphone", image1, false)
let actionSheet2 = AlertSheetModel.init("Speaker", "speaker", image2, false)
let actionSheet3 = AlertSheetModel.init("Samsung Level U", "samsung", image3, true)
AlertSheet.showActionSheet([actionSheet1,actionSheet2,actionSheet3], "", comletion: { (sharedClick: AlertSheetModel, success) in
if sharedClick.key == "iphone" {
// handle the click action of iphone button
}else if sharedClick.key == "speaker" {
// handle the click action of speaker button
} else if sharedClick.key == "samsung" {
// handle the click action of samsung button
}else {
}
}, onViewCotroller: self)
}
}