如何使用 Swift 显示和隐藏 UIAlertAction 按钮
How to show and hide UIAlertAction buttons using Swift
我正在实施导航栏按钮点击显示 UIAlertAction
和多个 buttons
。在这里,我需要根据 Switch
大小写输入值匹配来显示和隐藏 UIAlertAction 按钮。
我试过下面的代码,但不知道如何 hide
和 show
UIAlertAction 按钮。如何实现?
下面是我的代码
@IBAction func ClickAction(_ sender: Any) {
var index = 100
switch index {
case 100 : break
// Show Actionsheet button 1 & 2
case 10 : break
// Show Actionsheet button 4
case 5 : break
// Show Actionsheet button 3
default : break
// Show Actionsheet button 1 & 3
}
let actionSheetAlertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheetAlertController.addAction(cancelActionButton)
// Button #1
let oneActionButton = UIAlertAction(title: "One", style: .default, handler: {(action:UIAlertAction!) in
print("One")
})
actionSheetAlertController.addAction(oneActionButton)
oneActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
oneActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #2
let twoActionButton = UIAlertAction(title: "Two", style: .default, handler:{(action:UIAlertAction!) in
print("Two")
})
actionSheetAlertController.addAction(twoActionButton)
twoActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
twoActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #3
let threeActionButton = UIAlertAction(title: "Three", style: .default, handler:{(action:UIAlertAction!) in
print("Three")
})
actionSheetAlertController.addAction(threeActionButton)
threeActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
threeActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #4
let fourActionButton = UIAlertAction(title: "Four", style: .default, handler:{(action:UIAlertAction!) in
print("Four")
})
actionSheetAlertController.addAction(fourActionButton)
fourActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
fourActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
actionSheetAlertController.view.tintColor = #colorLiteral(red: 0.317096545, green: 0.5791940689, blue: 0.3803742655, alpha: 1)
self.present(actionSheetAlertController, animated: true, completion: nil)
}
将按钮添加到开关内的警报
let actionSheetAlertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheetAlertController.addAction(cancelActionButton)
// Button #1
let oneActionButton = UIAlertAction(title: "One", style: .default, handler: {(action:UIAlertAction!) in
print("One")
})
oneActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
oneActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #2
let twoActionButton = UIAlertAction(title: "Two", style: .default, handler:{(action:UIAlertAction!) in
print("Two")
})
twoActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
twoActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #3
let threeActionButton = UIAlertAction(title: "Three", style: .default, handler:{(action:UIAlertAction!) in
print("Three")
})
threeActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
threeActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #4
let fourActionButton = UIAlertAction(title: "Four", style: .default, handler:{(action:UIAlertAction!) in
print("Four")
})
fourActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
fourActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
actionSheetAlertController.view.tintColor = #colorLiteral(red: 0.317096545, green: 0.5791940689, blue: 0.3803742655, alpha: 1)
var index = 100
switch index {
case 100 :
// Show Actionsheet button 1 & 2
actionSheetAlertController.addAction(oneActionButton)
actionSheetAlertController.addAction(twoActionButton)
case 10 :
// Show Actionsheet button 4
actionSheetAlertController.addAction(fourActionButton)
case 5 :
// Show Actionsheet button 3
actionSheetAlertController.addAction(threeActionButton)
default :
// Show Actionsheet button 1 & 3
actionSheetAlertController.addAction(oneActionButton)
actionSheetAlertController.addAction(threeActionButton)
}
self.present(actionSheetAlertController, animated: true, completion: nil)
我正在实施导航栏按钮点击显示 UIAlertAction
和多个 buttons
。在这里,我需要根据 Switch
大小写输入值匹配来显示和隐藏 UIAlertAction 按钮。
我试过下面的代码,但不知道如何 hide
和 show
UIAlertAction 按钮。如何实现?
下面是我的代码
@IBAction func ClickAction(_ sender: Any) {
var index = 100
switch index {
case 100 : break
// Show Actionsheet button 1 & 2
case 10 : break
// Show Actionsheet button 4
case 5 : break
// Show Actionsheet button 3
default : break
// Show Actionsheet button 1 & 3
}
let actionSheetAlertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheetAlertController.addAction(cancelActionButton)
// Button #1
let oneActionButton = UIAlertAction(title: "One", style: .default, handler: {(action:UIAlertAction!) in
print("One")
})
actionSheetAlertController.addAction(oneActionButton)
oneActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
oneActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #2
let twoActionButton = UIAlertAction(title: "Two", style: .default, handler:{(action:UIAlertAction!) in
print("Two")
})
actionSheetAlertController.addAction(twoActionButton)
twoActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
twoActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #3
let threeActionButton = UIAlertAction(title: "Three", style: .default, handler:{(action:UIAlertAction!) in
print("Three")
})
actionSheetAlertController.addAction(threeActionButton)
threeActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
threeActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #4
let fourActionButton = UIAlertAction(title: "Four", style: .default, handler:{(action:UIAlertAction!) in
print("Four")
})
actionSheetAlertController.addAction(fourActionButton)
fourActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
fourActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
actionSheetAlertController.view.tintColor = #colorLiteral(red: 0.317096545, green: 0.5791940689, blue: 0.3803742655, alpha: 1)
self.present(actionSheetAlertController, animated: true, completion: nil)
}
将按钮添加到开关内的警报
let actionSheetAlertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheetAlertController.addAction(cancelActionButton)
// Button #1
let oneActionButton = UIAlertAction(title: "One", style: .default, handler: {(action:UIAlertAction!) in
print("One")
})
oneActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
oneActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #2
let twoActionButton = UIAlertAction(title: "Two", style: .default, handler:{(action:UIAlertAction!) in
print("Two")
})
twoActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
twoActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #3
let threeActionButton = UIAlertAction(title: "Three", style: .default, handler:{(action:UIAlertAction!) in
print("Three")
})
threeActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
threeActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
// Button #4
let fourActionButton = UIAlertAction(title: "Four", style: .default, handler:{(action:UIAlertAction!) in
print("Four")
})
fourActionButton.setValue(#imageLiteral(resourceName: "usa.png"), forKey: "image")
fourActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
actionSheetAlertController.view.tintColor = #colorLiteral(red: 0.317096545, green: 0.5791940689, blue: 0.3803742655, alpha: 1)
var index = 100
switch index {
case 100 :
// Show Actionsheet button 1 & 2
actionSheetAlertController.addAction(oneActionButton)
actionSheetAlertController.addAction(twoActionButton)
case 10 :
// Show Actionsheet button 4
actionSheetAlertController.addAction(fourActionButton)
case 5 :
// Show Actionsheet button 3
actionSheetAlertController.addAction(threeActionButton)
default :
// Show Actionsheet button 1 & 3
actionSheetAlertController.addAction(oneActionButton)
actionSheetAlertController.addAction(threeActionButton)
}
self.present(actionSheetAlertController, animated: true, completion: nil)