How to add a checkmark to a UIActionSheet button?

I wonder how to add a checkmark to the right of the ActionSheet button in the simplest way? Below is a screenshot of the Podcasts app.

enter image description here

+11
source share
5 answers

Please note that the solution may fail in a future update for iOS. I get access to undocumented private APIs. Such decisions are very fragile. Please see comments below.

Finally, I got the answer using the UIAlertController:

UIAlertController *customActionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *firstButton = [UIAlertAction actionWithTitle:@"First Button" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //click action }]; [firstButton setValue:[UIColor blackColor] forKey:@"titleTextColor"]; [firstButton setValue:[UIColor blackColor] forKey:@"imageTintColor"]; [firstButton setValue:@true forKey:@"checked"]; UIAlertAction *secondButton = [UIAlertAction actionWithTitle:@"Second Button" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //click action }]; [secondButton setValue:[UIColor blackColor] forKey:@"titleTextColor"]; UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ //cancel }]; [cancelButton setValue:[UIColor blackColor] forKey:@"titleTextColor"]; [customActionSheet addAction:firstButton]; [customActionSheet addAction:secondButton]; [customActionSheet addAction:cancelButton]; [self presentViewController:customActionSheet animated:YES completion:nil]; 

And here is the result:

UIActionSheet button check mark

+33
source

Swift Version: 4.1

I came across this implementation using extension creation via UIAlertController.

 extension UIAlertController { static func actionSheetWithItems<A : Equatable>(items : [(title : String, value : A)], currentSelection : A? = nil, action : @escaping (A) -> Void) -> UIAlertController { let controller = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) for (var title, value) in items { if let selection = currentSelection, value == selection { // Note that checkmark and space have a neutral text flow direction so this is correct for RTL title = "✔︎ " + title } controller.addAction( UIAlertAction(title: title, style: .default) {_ in action(value) } ) } return controller } 

}

Implementation:

  func openGenderSelectionPopUp() { let selectedValue = "Men" //update this for selected value let action = UIAlertController.actionSheetWithItems(items: [("Men","Men"),("Women","Women"),("Both","Both")], currentSelection: selectedValue, action: { (value) in self.lblGender.text = value }) action.addAction(UIAlertAction.init(title: ActionSheet.Button.cancel, style: UIAlertActionStyle.cancel, handler: nil)) //Present the controller self.present(action, animated: true, completion: nil) } 

Final result:

Select gender

Hope this helps!

thanks

+5
source

Another option is to simply add a check mark to the button title, for example, "Title ✓". This will be next to the name, not the right side of the button, but I think this is not a very big problem.

+2
source

Try this trick:

  • Learn how to show ViewController as a popup
    • Add UITable to ViewController
    • Show items in UITable
    • Customize UITable by adding custom cells
    • In each of the user cells add a button
    • This button will have two types of images, one empty box and another box with a check mark.
    • When the user touches a table cell, you need to change the button image corresponding to this table row, so the user believes that they check or uncheck
    • and finally add the Finish button at the bottom to close the view manager

Google all of these elements for tutorials. As I said, this is not an easy task, since Xcode does not have a checkmark function.

From: fooobar.com/questions/1012399 / ...

+1
source

Quick implementation:

 class Controller: UIViewController { var isFirstButtonChecked = true @IBAction func buttonTapped(_ sender: UIButton?) { let firstButton = UIAlertAction(title: "First Button", style: .default, handler: { [unowned self] _ in self.isFirstButtonChecked = true print("First Button tapped") }) //Here the main magic; it called Key-Value Coding, or KVC: firstButton.setValue(isFirstButtonChecked, forKey: "checked") let secondButton = UIAlertAction(title: "Second Button", style: .default, handler: { [unowned self] _ in self.isFirstButtonChecked = false print("Second Button tapped") }) secondButton.setValue(!isFirstButtonChecked, forKey: "checked") let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alert.addAction(firstButton) alert.addAction(secondButton) alert.addAction(cancel) self.present(alert, animated: true, completion: nil) } } 

You can also check the box for more buttons using the enumeration :

 class Controller: UIViewController { enum ButtonChecked { case first case second case third case forth } var buttonChecked: ButtonChecked = .first @IBAction func buttonTapped(_ sender: UIButton?) { let firstButton = UIAlertAction(title: "First Button", style: .default, handler: { [unowned self] _ in self.buttonChecked = .first print("First Button tapped") }) let secondButton = UIAlertAction(title: "Second Button", style: .default, handler: { [unowned self] _ in self.buttonChecked = .second print("Second Button tapped") }) let thirdButton = UIAlertAction(title: "Third Button", style: .default, handler: { [unowned self] _ in self.buttonChecked = .third print("Third Button tapped") }) let forthButton = UIAlertAction(title: "Forth Button", style: .default, handler: { [unowned self] _ in self.buttonChecked = .forth print("Forth Button tapped") }) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) switch buttonChecked { case .first: firstButton.setValue(true, forKey: "checked") case .second: secondButton.setValue(true, forKey: "checked") case .third: thirdButton.setValue(true, forKey: "checked") case .forth: forthButton.setValue(true, forKey: "checked") } let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alert.addAction(firstButton) alert.addAction(secondButton) alert.addAction(thirdButton) alert.addAction(forthButton) alert.addAction(cancel) self.present(alert, animated: true, completion: nil) } } 
0
source

Source: https://habr.com/ru/post/1012397/


All Articles