Help with conditional statement, please

Discussion in 'iOS Programming' started by HawaiiMacAddict, Jan 31, 2010.

  1. HawaiiMacAddict macrumors 6502a

    Joined:
    Dec 28, 2006
    Location:
    On one of my Macs of course
    #1
    Aloha everyone,

    I have an iPhone app nearing completion, but I need a little bit of help with a conditional statement. Basically, I have a credits page, where I have buttons that pull up the webpages of the the military branches (they are the source of my information). The URLs are passed to one UIWebView and everything works. I started off with one method per button, but wanted to use only one method for all buttons. Here is what I have currently:

    Code:
    - (IBAction)pressArmy:(id)sender {
    	creditWebView *credWeb = [[creditWebView alloc] initWithNibName:@"creditWebView"
    															 bundle:nil];
    	credWeb.urlAddress = @"http://www.army.mil/mobile";
    	[self.navigationController presentModalViewController:credWeb
    												 animated:YES];
    	[credWeb release];
    }
    
    - (IBAction)pressNavy:(id)sender {
    	creditWebView *credWeb = [[creditWebView alloc] initWithNibName:@"creditWebView"
    															 bundle:nil];
    	credWeb.urlAddress = @"http://www.navy.mil/m";
    	[self.navigationController presentModalViewController:credWeb
    												 animated:YES];
    	[credWeb release];
    }
    
    - (IBAction)pressAirForce:(id)sender {
    	creditWebView *credWeb = [[creditWebView alloc] initWithNibName:@"creditWebView"
    															   bundle:nil];
    	credWeb.urlAddress = @"http://www.airforce.com";
    	[self.navigationController presentModalViewController:credWeb
    												 animated:YES];
    	[credWeb release];
    }
    - (IBAction)pressMarineCorps:(id)sender {
    	creditWebView *credWeb = [[creditWebView alloc] initWithNibName:@"creditWebView"
    															 bundle:nil];
    	credWeb.urlAddress = @"http://www.marines.mil";
    	[self.navigationController presentModalViewController:credWeb
    												 animated:YES];
    	[credWeb release];
    }
    
    As you can see, there's quite a bit of repetition, hence the conditional statement approach, shown below:

    Code:
    - (IBAction)pressCredits:(id)sender {
    	creditWebView *credWeb = [[creditWebView alloc] initWithNibName:@"creditWebView"
    															 bundle:nil];
    	if (usn) {
    		credWeb.urlAddress = @"http://www.navy.mil/m";
    	}
    	else if (usaf) {
    		credWeb.urlAddress = @"http://www.airforce.com";
    	}
    	else if (usa) {
    		credWeb.urlAddress = @"http://www.army.mil/mobile";
    	}
    	else if (usmc) {
    		credWeb.urlAddress = @"http://www.marines.mil";
    	}
    
    		[self.navigationController presentModalViewController:credWeb
    												 animated:YES];
    	[credWeb release];
    }
    
    This is much simpler and takes a lot less code than the other methods above. Oh, and in the web view controller class (in the viewDidLoad method), here's the pertinent code fragment:

    Code:
    	NSURL *url = [NSURL URLWithString:urlAddress];
    	NSLog(@"urlAddress returns: %@", urlAddress);
    	
    	NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    	[webView loadRequest:requestObj];
    	[urlBar setText:urlAddress];
    I allocated and initialized the credViewController class in my CreditsView class, so that I could use the urlAddress variable to pass in the URL strings. With respect to the separate methods, everything works, but with the conditional method, the only URL pulled is the Navy one. The conditions are the names of the buttons. Since they are all connected to the same method, I though that was the best way to accomplish my goal.

    Can anyone see where I've made my mistake? The only thing comes to mind is that the conditions are not set correctly in the conditional method. As mentioned earlier, I would much prefer using that one, due to its brevity, but I am using what works, for now.

    Thank you in advance for any and all reponses
     
  2. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #2
    I think when I did something similar I had to cast the sender as the button and then check it's tag (which I set in IB). Sort of like this:
    Code:
    - (IBAction)pressCredits:(id)sender {
         UIButton *tmpBtn = (UIButton *)sender;
         ...
         switch(tmpBtn.tag) {
              case 1:
                   credWeb.urlAddress = @"http://www.navy.mil/m";
                   break;
                   ... // etc
         }
    }
    
    Again, I think that's what I did. That was a while ago and I have no code in front of me at the moment.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Show the declarations for the variables usn, usaf, usa, and usmc. If they're valid objects of any type, then they are all non-zero (non-nil), so the first one tested will be true, and none of the if-else's will be evaluated. You'll need to rethink what condition you're testing in each if statement.

    Or you could refactor it so the common code is in a single method, and the url string is passed as a parameter. Something like this (untested; written after 2 pints):

    Code:
    - (IBAction)pressArmy:(id)sender {
      [self pressed:@"http://www.army.mil/mobile"];
    }
    
    - (IBAction)pressArmy:(id)sender {
      [self pressed:@"http://www.navy.mil/m"];
    }
    
    -(void) pressed:(NSString*)urlString {
    	creditWebView *credWeb = [[creditWebView alloc] initWithNibName:@"creditWebView" bundle:nil];
    	credWeb.urlAddress = urlString;
    	[self.navigationController presentModalViewController:credWeb animated:YES];
    	[credWeb release];
    }
    
     
  4. HawaiiMacAddict thread starter macrumors 6502a

    Joined:
    Dec 28, 2006
    Location:
    On one of my Macs of course
    #4
    Aloha fishkorp and chown33,

    Thanks for your replies. I'm now using the suggestion provided by fishkorp. I was originally wanting to try the switch method, but didn't think of the tag idea. Thanks for that :)

    Now everything works a lot better and I can get rid of some unnecessary code and xibs :D The more I learn about coding for the iPhone, the more I learn how much I don't know. Thanks again for both of your replies.
     

Share This Page