views:

77

answers:

5

After reading the iPhone Human Interface Guidelines, I notice there's little mention of checkboxes in the style that one may encounter them on the desktop or web.

Checkboxes are generally handled by UISwitchs on the iPhone, but for an app I'm presently working on, they're really not the right control. Instead, the control you'll see in Mail is a much better fit:

Checkboxes in Mail

Actual mail blanked out. Obviously.

How would I go about using these checkbox controls in my app? Are they standard, or will I need to imitate them with a custom control?

Cheers friends.

+1  A: 

I'm pretty certain there is no standard way to do this. However it's fairly simple to achieve, all you need is two images, one for each state. I would probably do something simple like subclass UIImageView and add a setState:(BOOL)theState method, which would then simply select the relevant image.

alku83
+3  A: 

You'll need to create a custom control. It won't be difficult since UIControl already has 'selected', 'highlighted' and 'state' properties at your disposal. You'll just need to draw and toggle appropriately.

Jasarien
+1  A: 

I'd rather subclass UITableViewCell then UIImageView. UITableViewCell allready comes with selected/unselected states and handlers for editmodes etc.

samsam
+1  A: 

As said before, you'll need to subclass UIControl. The actual process was discussed here w little while ago.

paxswill
+2  A: 

Don't subclass UIControl. What you want is a UIButton of "custom" type. Load it with your "unlit" image in IB (or programmatically in -viewDidLoad--you can set it appropriate to its data there too, if you came here with that property already "checked").

Point its touchUpInside event at a method called -(void)toggleCheckBox, and in that method, toggle whatever setting you're toggling (probably a BOOL property of the objects you're listing), and toggle the "lit/unlit" status of the button image by using its -setImage: forState: method. Use the control state UIControlStateNormal.

I do something similar where I let people poke a button to toggle the "favorite" status of the thing ("thisEvent"--a member of an array of local cultural/arts events) they're looking at:

- (IBAction)toggleFavorite {

    if (self.thisEvent.isFavorite == YES) {
        self.thisEvent.isFavorite = NO;
        [self.favoriteButton setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
    }
    else {
        self.thisEvent.isFavorite = YES;
        [self.favoriteButton setImage:[UIImage imageNamed:@"isFavorite.png"] forState:UIControlStateNormal];

    }
}
Dan Ray
+1, although testing a BOOk against YES is arguable, and I'd make it '-(IBAction)toggleFavorite:(id)sender' and '[sender setImage:...]'.
Eiko
Maybe so re using (id)sender. I like to be explicit about testing bools against YES--I find it much clearer reading it later.
Dan Ray
I have had some great answers to this question but I'll have to give this one to you Dan. Thanks for your answer; this is exactly how I ended up doing it and it works wonderfully.
David Foster