最佳答案
公共(public)类 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 { 公共(public)变量标题:字符串 公钥:字符串 公共(public)变量图像:UIImage? 公共(public)变量 isSelected: Bool = false 公共(public)变量对象:有吗? public init(_ title: String,_ key: String, _ image: UIImage?, _ isSelected: Bool) { self.title = 标题 self.key = 键 self.isSelected = isSelected self.image = 图片 }
类 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)
}
关于ios - 获取音频选项并在内部切换它们以调用应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58946849/