开发者

Cocoa-Touch UIButton isSelected clarification

I'm rather new to programming in Cocoa, but I've been working on learning the language quite diligently up until I hit this snag that I can't seem to circumvent/hack my way around, (not that I'd want to. I'd prefer to d开发者_如何学Co it right!)

Where I stand, In IB I have a toolbar that has a button and what I'm trying to do is mimic the maps app. I want to be able to press the button and then have my location pop up, while keeping the button selected, then when it's pressed again, deselect it and thus remove the blue blip location from the map.

ideally, I would like to use the following code, but the if statement doesn't seem to want to work on the simulator (which I presume wouldn't change if I tried on the iPhone.)

-(IBAction) showLocation: (id) sender
{
 if([sender isSelected]) // this doesn't work!!
 {
  [sender setSelected:NO];
  mapView.showsUserLocation = FALSE;
 }
 else
 {
  [sender setSelected:YES];
  mapView.showsUserLocation = TRUE; 
 }
}

obviously if I get rid of the if statement, I know that I can show the location and set the selected as I liked, but I can't seem to "get" the selected property from the button... what's the right way of doing this?


try

- (void)methodName:(UIButton *)sender
{
if (sender.selected == YES) ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜