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:
As you can see, there's quite a bit of repetition, hence the conditional statement approach, shown below:
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:
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
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