开发者

Trouble using an if within an IBAction

Apologies if this is a total newbie question, I have looked around online and couldn't find a simple answer.

I'm trying to make a navigation bar button disappear if a field ("googlemap") isn't filled in my iPhone app. Thinking it would just be an if statement, I've used the following code, which resulted in the button losing all of its functionality (it calls another view) and not disappearing.

-(IBAction)btnMapM
{
 if (googlemap != nil)
 {

 LocationInfo *ObjLocInfo = [self.LocationInfoArr objectAtIndex:0];
 LocationMap *ObjLocationMap = [[LocationMap alloc] initWithNibName:@"LocationMap" bundle:nil];
 ObjLocationMap.address =  ObjLocInfo->address;
 ObjLocationMap.latitude = ObjLocInfo->latitude;
 ObjLocationMap.longtitude = ObjLocInfo->longtitude;



 [self.navigationController pushViewController:ObjLocationMap animated:YES];
 [ObjLocationMap release];

}
}

Is it not as simple as I had thought? If not, I'd really appreciate it if someone has the time to point me in the right direction... :-S

Thanks,

开发者_开发百科

Nic


I'll answer this assuming that, if a particular property is set to nil, you want your button to disappear. (Check my answer here for a better way to check if a property is empty.)

First, make sure you've declared an outlet to the button, using

IBOutlet UIButton *button;

in the header file, and connected everything properly in interface builder. This includes connecting your button to the button outlet, and the touchUpInside event to your IBAction. Then, use this for your viewDidLoad method:

-(void) viewDidLoad {
    [super viewDidLoad];
    if (googlemap == nil) {
        [button setHidden:YES];
        [button setEnabled:NO];
    }
}

Using this, your app checks the googlemap property when the view loads, and hides the button if googlemap is empty. Otherwise, it proceeds, business as usual.

You can go ahead and remove the if statement from the IBAction, as that method only gets called when the user presses the button. If the button's hidden, then, the method never gets called.


Try

-(IBAction)btnMapM: (id)sender
{
  ...

and also @Sam Ritchie's answer.


Thank you to everyone who helped answer this - it turned out that my navigation bar button was declared separately in viewDidLoad (btnMapm was an action method, not the actual button, which took me a while to realise) so I just edited Sam's if statement in viewDidLoad to make it hide as follows:

    if (googlemap != nil) {

        BlueButton *blueSaveButton = [[BlueButton alloc] init];
        [blueSaveButton setTitle:@"Map" forState:UIControlStateNormal];
        [blueSaveButton addTarget:self action:@selector(btnMapM) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *button = [[UIBarButtonItem alloc]  initWithCustomView:blueSaveButton];
        self.navigationItem.rightBarButtonItem = button;
        [button release];
        [blueSaveButton release];
    }

And figuring that out took me about three hours. Being a beginner sucks... But perhaps this will help someone else!

Thanks

Nick

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜