Search code examples
objective-ccocoa-touchif-statementios7nsurl

If Statement, hide button


I am trying to evaluate a string for a particular url contained within a dictionary, and then hide the rightbarbutton if that url is the one currently contained in the "url" variable. Using NSLogs, I found that the url string in question is loaded into "url", but the if statement never occurs. I'm a bit of a novice, and would appreciate some feedback on how to appropriately structure this. Thanks a million!

NSURL *url = [NSURL URLWithString:[self.webViewDict valueForKey:@"Url"]];
            NSLog(@"%@",url);

if ([url isEqual:@"URLGOESHERE"]){
    NSLog(@"If statement called");
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor   clearColor]}
                                                                      forState:UIControlStateDisabled];
     self.navigationItem.rightBarButtonItem.enabled = NO;
}

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

Solution

  • The problem is you're comparing an NSURL object with an NSString object. Of course they will never be equal. You need to do something like this:

    if([[url absoluteString] isEqualToString:@"http://gacc.nifc.gov/rmcc/predictive/outlook/Rocky_Mountain_Area_Outlook2014.pdf"])
    {
    // equal!
    }