1

Ok, so the following code gets the title of a page loaded in a UIWebView and then checks to see if the title contains the word "table". If it does then it unhides the toolbar. If it doesn't then it hides the tool bar. This works fine at first when a page without "table" in the title loads and when you browse to one that does have "table" in the title it shows the toolbar. The problem occurs when you go back to a page without "table" in the title, the new title appears in the NSLog, but the toolbar doesn't disappear.

NSString * webtitle = [viewWeb stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"Title is: %@", webtitle);
if ([webtitle rangeOfString:@"Table"].location == NSNotFound) {
    [toolbar setHidden:YES];
} else {
    [toolbar setHidden:NO];
}

Any help is appreciated.

Thanks!

EDIT: I should have said that I am executing this within

  - (void)webViewDidFinishLoad:(UIWebView *)wv
  {
  }

EDIT 2: I have now added in NSLogs to fire when the if runs:

if ([webtitle rangeOfString:@"Table"].location == NSNotFound) {
    NSLog(@"Hidden");
    [toolbar setHidden:YES];
} else {
    NSLog(@"Not Hidden");
    [toolbar setHidden:NO];
}

"Hidden" is returned when you browse to a page without "table" but the toolbar is still displayed so it looks as thought [toolbar setHidden:YES]; isn't working for some reason. Does anybody have any clues?

Cezar
  • 55,636
  • 19
  • 86
  • 87
Compy
  • 1,157
  • 1
  • 12
  • 24

2 Answers2

1

Contrary to what many people expect, webViewDidFinishLoad: may be called several times before the page has actually finished loading. Actually, it is called everytime the webview finishes loading a frame. From the documentation:

webViewDidFinishLoad:
Sent after a web view finishes loading a frame.

That being said, try debugging with breakpoints. Add one breakpoint to the [toolbar setHidden:YES]; line and another one on [toolbar setHidden:NO]; and figure out which one of them is being called last. I am guessing it might be the second one.

Cezar
  • 55,636
  • 19
  • 86
  • 87
  • Ok, I tried this and it seems to execute [toolbar setHidden:YES]; last. It doesn't actually execute it though! I also tried [toolbar removeFromSuperview]; and it doesn't work after the bar has appeared either. I really don't understand what is going on here. – Compy Mar 20 '13 at 13:09
  • @Adam is your ViewController in a NavigationController? – Cezar Mar 20 '13 at 13:14
0

I'm not sure if you are navigating within the webview or within your view controllers.

If you are navigating within your view controllers, then you could also check for that title logic in

 -(void)viewWillAppear {
 }

or

 -(void)viewDidAppear {
 }

when you return back to the page

if you are navigating within your web view - clicking on links, etc, you might need to subclass a UIWebView and override the following methods and check for your title in them:

 -(void)goBack {
     [super goBack];
      //perform check here
 }

 -(void)goForward {
 }

hope that helps

CocoaEv
  • 2,984
  • 20
  • 21