views:

29

answers:

1

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;
}

}

A: 

In the webView:shouldStartLoadWithRequest you never pass the URL to the "NavigationViewController" instance you create. It can't work...

So every time the viewDidLoad is called you call one of your 4 URL depending on the tabBar state.

Here is what you should do :

in NavigationViewController :

  • Add an "NSURL *pageURL" attribute
  • Create a good init method :
-(id)initWithURL:(NSURL *)url
{
    if ([super initWithNibName:@"NavigationView" bundle:nil])
    {
       _pageURL = [url retain];
    }
    return self;
}

And in viewDidLoad:

if (_pageURL == nil) {
// 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) {
    _pageURL = [ NSURL URLWithString: @"http://mobile.example.com/schedule" ];    
}
else if (self.tabBarController.selectedIndex == 2) {
    _pageURL = [ NSURL URLWithString: @"http://mobile.example.com/roster" ];  
}
else if (self.tabBarController.selectedIndex == 3) {
    _pageURL = [ NSURL URLWithString: @"http://mobile.example.com/stats" ];   
}
else if (self.tabBarController.selectedIndex == 4) {
    _pageURL = [NSURL URLWithString:@"http://mobile.example.com/about" ];   
}
}

[webView loadRequest:[NSURLRequest requestWithURL:_pageURL]];

// Loads the super class.
[super viewDidLoad];

And then initialize correctly your instance in webView:shouldStartLoadWithRequest

NavigationViewController *newView = [[NavigationViewController alloc] initWithURL:request.URL];

Add to the top

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

NSURL *url = request.URL;

NSString *urlString = url.absoluteString;

if ([urlString isEqualToString:_pageURL.absoluteString]){return YES;}

...

F.Santoni
I added the additional code you suggested and then get this error on the [webView loadRequest:_pageurl]; line: warning: incompatible Objective-C types 'struct NSURL *', expected 'struct NSURLRequest *' when passing argument 1 of 'loadRequest:' from distinct Objective-C type
SalsaSalsa
Changing the type of NSURL to NSURLRequest just make it crash.
SalsaSalsa
Use your brain ;-) just create a request with the url ... I updated the code I provided you. You will also have to change the first initialization...
F.Santoni
You'll have to forgive my ignorance I've worked with Obj-C and the iPhone SDk for less than a week or so now. Also, that created an infinite loop :(
SalsaSalsa
for the loop in viewDidLoad put "webView.delegate = self;" at the end (just before [super viewDidLoad])
F.Santoni
Still stuck in the infin loop :O btw I appreciate you being so helpful.
SalsaSalsa
Any idea why it keep looping?
SalsaSalsa
I cannot for the life of me figure out why it keeps looping :(
SalsaSalsa
try adding if ([urlString isEqualToString:_pageURL.absoluteString]){return YES;} (and change viewDidLoad as I did)
F.Santoni