Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UIActionSheet button check mark?

I wonder how to add check mark to the right of actionSheet button the simplest way? Bellow is a screenshot of Podcasts app.

enter image description here

like image 590
Jared Chu Avatar asked Nov 17 '16 04:11

Jared Chu


People also ask

How do I add check boxes and option buttons to forms?

To add an option button, click the Developer tab, click Insert, and under Form Controls, click. Click in the cell where you want to add the check box or option button control. Tip: You can only add one checkbox or option button at a time. To speed things up, after you add your first control, right-click it and select Copy > Paste.

How do you put a check mark on a page?

The bullet list tool is another way to insert check marks on your page. Simply highlight the text you want to bullet, or you can insert bullet lists and add text as you complete your document. To make the bullet points display as checkmarks, use the formatting tool.

How do I add a checkmark to a cell in Excel?

Select a cell in your workbook where you want to add a checkmark or tickmark then go to the Insert tab and press the Symbol command. This will open up the Symbol dialog box and a long list of symbols will be presented, but you will not find a checkmark in the hundreds of options presented in the default font.

How do you check if an option button is checked or unchecked?

Checked: Displays an option button that is selected. Unchecked: Displays an option button that is cleared. In the Cell link box, enter a cell reference that contains the current state of the option button. The linked cell returns the number of the selected option button in the group of options. Use the same linked cell for all options in a group.


4 Answers

Note that the solution can crash in a future update to iOS. I'm accessing undocumented private APIs. Such solutions are very fragile. Please see the comments below.

Finally I got the answer by using 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 this is the result:

UIActionSheet button check mark

like image 61
Jared Chu Avatar answered Oct 09 '22 09:10

Jared Chu


Swift Version: 4.1

I came across this implementation using creating extension over 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 that helps!

Thanks

like image 40
Harjot Singh Avatar answered Oct 09 '22 11:10

Harjot Singh


Another option is just to add a check mark character to a button title, like this "Title ✓". It will be next to the title, not at the right side of the button, but I think it's not a very big problem.

like image 44
tagirkaZ Avatar answered Oct 09 '22 11:10

tagirkaZ


Swift 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's the main magic; it's 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 provide checkmark for bigger number of buttons using an enum:

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)
    }
}
like image 35
Ostap4ik Avatar answered Oct 09 '22 09:10

Ostap4ik