Okay, what I currently have is a TabBarController with five tabs. Each of these tabs are UINavigationControllers. Each of the Views associated with the tabs link to a XIB file that contains a View with a UIWebView. What I want to happen is when a link is clicked in the UIWebView a new navigation view (with a back button) is pushed onto the stack and the content be filled with the link that was clicked, what actually happens is close but no cigar. It loads the original page I left from, for example: I'm on www.example.com and I click a link and the new view loads (with the back button) and just reloads www.example.com :(
Also, I have a check in the viewDidLoad method to determine which tab is selected which in turns tell what content needs to be present.
Here is my code:
- (void)viewDidLoad {
// Allows webView clicks to be captured.
webView.delegate = self;
// Places statsheet image centered into the top nav bar.
UIImage *image = [UIImage imageNamed: @"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
// Loads webpage according to the tab selected.
if (self.tabBarController.selectedIndex == 0) {
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com" ] ] ];
}
else if (self.tabBarController.selectedIndex == 1) {
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/schedule" ] ] ];
}
else if (self.tabBarController.selectedIndex == 2) {
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/roster" ] ] ];
}
else if (self.tabBarController.selectedIndex == 3) {
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/stats" ] ] ];
}
else if (self.tabBarController.selectedIndex == 4) {
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/about" ] ] ];
}
// Loads the super class.
[super viewDidLoad];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSRange page = [ urlString rangeOfString: @"/?page=" ];
NSRange post = [ urlString rangeOfString: @"/posts/" ];
NSRange notBlog = [ urlString rangeOfString: @"example" ];
// Allow webapge to load if next or prev button is clicked.
if ( page.location != NSNotFound ) {
return YES;
}
// Pass post link to new view with a back navigation button.
else if ( post.location != NSNotFound) {
NavigationViewController *newView = [[NavigationViewController alloc] initWithNibName:@"NavigationView" bundle:nil];
// ^^^^^^^^^^^^^^^^^^ Here is where the new view is pushed but doesnt load the correct page
[self.navigationController pushViewController:newView animated:YES];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[newView release];
return NO;
}
// Allow all links that are a part of the five main pages to be loaded.
else if ( notBlog.location != NSNotFound) {
return YES;
}
//Allows everything else to be loaded into a new view with a back navigation button.
else {
NavigationViewController *newView = [[NavigationViewController alloc] initWithNibName:@"NavigationView" bundle:nil];
// ^^^^^^^^^^^^^^^^^^ Here is where the new view is pushed but doesnt load the correct page
[self.navigationController pushViewController:newView animated:YES];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[newView release];
return NO;
}
}