Disable back/forward button in web browser
In my app, I have a UIWebView. Beneath it, I have a toolbar with the back and forward buttons to control th开发者_JAVA百科e browser. How do I disable the back button when the user is on the initial page and disable the forward button when the user is on the most recent page? Looking at the UIWebViewDelegate protocol, I don't see any callbacks that tell me the position of the user in their browser history.
It should look like Safari's toolbar:
Edit: I figured out the detection part, but I can't figure out the disabling part. My buttons always remain at full opacity and are tappable even when NSLog
prints out 0
for canGoBack
or canGoForward
.
.h
@interface VeetleViewController : UIViewController <UIWebViewDelegate> {
UIWebView* webView;
UIBarButtonItem* buttonBack;
UIBarButtonItem* buttonForward;
UIActivityIndicatorView* activityIndicator;
}
- (void)activityIndicatorStop;
@property (nonatomic, retain) IBOutlet UIWebView* webView;
@property (nonatomic, retain) IBOutlet UIBarButtonItem* buttonBack;
@property (nonatomic, retain) IBOutlet UIBarButtonItem* buttonForward;
@property (nonatomic, retain) UIActivityIndicatorView* activityIndicator;
@end
.m
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"canGoBack:%d canGoForward:%d", self.webView.canGoBack, self.webView.canGoForward);
[self.activityIndicator startAnimating];
if (self.webView.canGoBack) {
self.buttonBack.enabled = YES;
} else {
self.buttonBack.enabled = NO;
}
if (self.webView.canGoForward) {
self.buttonForward.enabled = YES;
} else {
self.buttonForward.enabled = NO;
}
}
It's properties on the UIWebView, not the delegate.
see: canGoBack and canGoForward.
http://developer.apple.com/library/ios/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIWebView/canGoBack
Just implement in one of the delegate methods of web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[backbutton setEnabled:[webView canGoBack]];
[fwdbutton setEnabled:[webView canGoForward]];
}
精彩评论