iphone - Passing URL, via UIWebView click, incorrectly -


okay, have tabbarcontroller 5 tabs. each of these tabs uinavigationcontrollers. each of views associated tabs link xib file contains view uiwebview. want happen when link clicked in uiwebview new navigation view (with button) pushed onto stack , content filled link clicked, happens close no cigar. loads original page left from, example: i'm on www.example.com , click link , new view loads (with button) , reloads www.example.com :(

also, have check in viewdidload method determine tab selected in turns tell content needs present.

here code:

- (void)viewdidload { // allows webview clicks captured. webview.delegate = self; // places statsheet image centered top nav bar. uiimage *image = [uiimage imagenamed: @"header.png"]; uiimageview *imageview = [[uiimageview alloc] initwithimage: image]; self.navigationitem.titleview = imageview; [imageview release];  // loads webpage according 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 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 load if next or prev button clicked. if ( page.location != nsnotfound ) {     return yes; } // pass post link new view navigation button.  else if ( post.location != nsnotfound) {     navigationviewcontroller *newview = [[navigationviewcontroller alloc] initwithnibname:@"navigationview" bundle:nil];     // ^^^^^^^^^^^^^^^^^^ here new view pushed doesnt load 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 links part of 5 main pages loaded. else if ( notblog.location != nsnotfound) {     return yes; } //allows else loaded new view navigation button. else {     navigationviewcontroller *newview = [[navigationviewcontroller alloc] initwithnibname:@"navigationview" bundle:nil]; // ^^^^^^^^^^^^^^^^^^ here new view pushed doesnt load 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; } 

}

in webview:shouldstartloadwithrequest you never pass url "navigationviewcontroller" instance create. can't work...

so every time viewdidload called call 1 of 4 url depending on tabbar state.

here should :

in navigationviewcontroller :

  • add "nsurl *pageurl" attribute
  • create 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 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 super class. [super viewdidload]; 

and initialize correctly instance in webview:shouldstartloadwithrequest

navigationviewcontroller *newview = [[navigationviewcontroller alloc] initwithurl:request.url]; 

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

...


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -