Saturday, 20 October 2012

How to make a check button in the application without using image

In iOS application Xcode does not have an object of check button, which is available only in Mac applications. We can create a check button in the application by using unicode string.
The unicode string for check mark button is : 2611
and unicode string for uncheck button is : 2610

Using this string we can have a check mark or uncheck mark in the application, which is used for different states in UIButton.
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateNormal];
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateHighlighted];
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateDisabled];
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateNormal];
[_checkButton setTitle:[NSString stringWithString:@"\u2611"] forState:UIControlStateSelected];

This code can be wrtiten in viewDidLoad, where the initialization is done. And the state can be changed in the action method of this button, as shown in below method.
- (IBAction) click:(UIButton*)sender
{
sender.selected = !sender.selected;
}

As we have to change the state to selected for check, this line will do the work for that.

To identify the check or uncheck of the button, we need to just check the selected state of the button wether its YES or NO.

Happy Coding.

1 comment: