Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Make the back and forward buttons only enabled when the webview can a…
Browse files Browse the repository at this point in the history
…ctually go back or forward.
  • Loading branch information
hartman committed Aug 8, 2011
1 parent b7c2819 commit f29b217
Show file tree
Hide file tree
Showing 9 changed files with 4,354 additions and 2,925 deletions.
11 changes: 9 additions & 2 deletions Classes/RootViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
Wikipedia_MobileAppDelegate *appDelegate;

UIWebView *webView;
IBOutlet UISearchBar *searchBar;
UISearchBar *searchBar;
UIToolbar *toolBar;
UIBarButtonItem *backButton;
UIBarButtonItem *forwardButton;

NSString *pageTitle;
UIView *shade;
UITableView *tableView;
Expand All @@ -29,7 +33,10 @@
@property (nonatomic, retain) Wikipedia_MobileAppDelegate *appDelegate;

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
@property (nonatomic, retain) IBOutlet UIToolbar *toolBar;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
@property (nonatomic, retain) NSString *pageTitle;
@property (nonatomic, retain) IBOutlet UIView *shade;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
Expand Down
17 changes: 14 additions & 3 deletions Classes/RootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@implementation RootViewController

@synthesize webView, searchBar, searchResults;
@synthesize webView, searchBar, searchResults, toolBar, backButton, forwardButton;
@synthesize appDelegate, pageTitle, shade, tableView;

@synthesize managedObjectContext;
Expand Down Expand Up @@ -47,6 +47,9 @@ - (void)viewDidLoad {
searchBar.showsScopeBar = NO;
searchBar.frame = CGRectMake(0, 0, 320.0f, 44.0f);

backButton.enabled = NO;
forwardButton.enabled = NO;

[self loadStartPage];

self.managedObjectContext = appDelegate.managedObjectContext;
Expand Down Expand Up @@ -118,7 +121,9 @@ - (void)handleTimer:(NSTimer *)timer
}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
#pragma mark WebViewDelegate

- (void)webView:(UIWebView *)awebView didFailLoadWithError:(NSError *)error {
[timer invalidate];
if (error != nil) {
NSString *errorString = [NSString stringWithFormat:@"%@", error];
Expand All @@ -133,9 +138,12 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[HUD hide:YES];
}

self.backButton.enabled = awebView.canGoBack;
self.forwardButton.enabled = awebView.canGoForward;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
- (void)webViewDidFinishLoad:(UIWebView *)awebView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

pageTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
Expand All @@ -151,6 +159,9 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
if (![pageTitle isEqualToString:@"Wikipedia"] && ![pageTitle isEqualToString:nil]) {
[self addRecentPage:pageTitle];
}

self.backButton.enabled = awebView.canGoBack;
self.forwardButton.enabled = awebView.canGoForward;
}

- (void)addRecentPage:(NSString *)pageName {
Expand Down
6 changes: 6 additions & 0 deletions Classes/WikiViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

NSString *wikiEntryURL;
UIWebView *webView;
UIToolbar *toolbar;
UIBarButtonItem *backButton;
UIBarButtonItem *forwardButton;
MapViewController *superView;
NSString *pageTitle;
MBProgressHUD *HUD;
Expand All @@ -27,6 +30,9 @@

@property (nonatomic, retain) NSString *wikiEntryURL;
@property (nonatomic, retain) NSString *pageTitle;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) MapViewController *superView;

Expand Down
16 changes: 12 additions & 4 deletions Classes/WikiViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@implementation WikiViewController

@synthesize appDelegate, wikiEntryURL, webView, superView;
@synthesize appDelegate, wikiEntryURL, webView, superView, toolbar, backButton, forwardButton;
@synthesize pageTitle;
@synthesize managedObjectContext;

Expand All @@ -39,9 +39,11 @@ - (void)viewDidLoad {
self.managedObjectContext = appDelegate.managedObjectContext;

[webView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:@"UITexture2.png"]]];
backButton.enabled = NO;
forwardButton.enabled = NO;

NSMutableURLRequest *URLrequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:wikiEntryURL]];
[URLrequest setValue:@"Wikipedia Mobile/2.0" forHTTPHeaderField:@"User_Agent"];
[URLrequest setValue:@"Wikipedia Mobile/2.0" forHTTPHeaderField:@"User-Agent"];

[webView loadRequest:URLrequest];
}
Expand All @@ -52,17 +54,20 @@ - (void)webViewDidStartLoad:(UIWebView *)webView {
[self showLoadingHUD];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
- (void)webView:(UIWebView *)awebView didFailLoadWithError:(NSError *)error {
if (error != nil) {
NSString *errorString = [NSString stringWithFormat:@"%@", error];
NSLog(@"%@", errorString);

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[HUD hide:YES];
}

self.backButton.enabled = awebView.canGoBack;
self.forwardButton.enabled = awebView.canGoForward;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
- (void)webViewDidFinishLoad:(UIWebView *)awebView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

pageTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
Expand All @@ -72,6 +77,9 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
if (![pageTitle isEqualToString:@"Wikipedia"] && ![pageTitle isEqualToString:nil]) {
[self addRecentPage:pageTitle];
}

self.backButton.enabled = awebView.canGoBack;
self.forwardButton.enabled = awebView.canGoForward;
}


Expand Down
Loading

0 comments on commit f29b217

Please sign in to comment.