PDA

View Full Version : How do I gray out UIWeb Back Button and Forward Buttons?




troop231
Apr 5, 2011, 02:42 PM
Hi, I seem a little lost on this, what I want to do is make the forward and back buttons in my webView gray out if there is no browsing activity, just like in Safari. Screenshot and code:

http://i53.tinypic.com/2ikv4m9.png

Web.h:

#import <UIKit/UIKit.h>
@interface Web: UIViewController {
UIWebView *webView;
UIActivityIndicatorView *activityIndicator;
NSTimer *timer;
}


@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) NSTimer *timer;
@end
and

Web.m:

#import "Web.h"


@implementation Web

@synthesize webView;
@synthesize activityIndicator;
@synthesize timer;

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation !=
UIInterfaceOrientationPortraitUpsideDown);
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[webView addSubview: activityIndicator];
self.title = @"MacRumors";

NSString *urlAddress = @"http://www.macrumors.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];




}
-(void)loading{
if(!webView.loading)
[activityIndicator stopAnimating];

else
[activityIndicator startAnimating];
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}


- (void)dealloc {

[webView release];
[activityIndicator release];
[timer release];
[super dealloc];
}


@end



ulbador
Apr 5, 2011, 02:58 PM
Learn to use the iOS documentation:


http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html


See:


canGoBack
A Boolean value indicating whether the receiver can move backward. (read-only)


canGoForward
A Boolean value indicating whether the receiver can move forward. (read-only)

loading
A Boolean value indicating whether the receiver is done loading content. (read-only)





Then you can just disable the button and change the alpha to make it grayed out and unclickable depending on what your webview is doing.

troop231
Apr 5, 2011, 03:20 PM
Learn to use the iOS documentation:


http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html


See:


canGoBack
A Boolean value indicating whether the receiver can move backward. (read-only)


canGoForward
A Boolean value indicating whether the receiver can move forward. (read-only)

loading
A Boolean value indicating whether the receiver is done loading content. (read-only)





Then you can just disable the button and change the alpha to make it grayed out and unclickable depending on what your webview is doing.

I added the two properties in my .h file, and disabled the buttons in IB, however they don't work at all now.

ulbador
Apr 5, 2011, 03:51 PM
Well, I'm a little confused about your level of experience. You say you added the properties. Do you mean you added the outlets to your forward/backward buttons and linked them up in interface builder?

You would have to enable the buttons when the view loaded, disable them when you start loading the URL, and then reenable them as needed when the webview is done loading (when you disable the activityview).

You also never stop the timer in the above code, which seems to me like it would be terribly inefficient.

PhoneyDeveloper
Apr 5, 2011, 04:02 PM
You need to write a method called say updateButtons. In this method you set the enabled properties of the two buttons based on the webview's canGoBack/forward. Then you need to call this method in the appropriate places in your code. Like when the view loads and when the user taps the forward/backward buttons and anyplace else that a change can occur in the webview.

troop231
Apr 5, 2011, 05:33 PM
Well, I'm a little confused about your level of experience. You say you added the properties. Do you mean you added the outlets to your forward/backward buttons and linked them up in interface builder?

You would have to enable the buttons when the view loaded, disable them when you start loading the URL, and then reenable them as needed when the webview is done loading (when you disable the activityview).

You also never stop the timer in the above code, which seems to me like it would be terribly inefficient.

Excuse me? The timer stops perfectly...

I also connected the buttons to the uiwebview in ib

ulbador
Apr 5, 2011, 06:01 PM
Excuse me? The timer stops perfectly...

I also connected the buttons to the uiwebview in ib

The indicator may stop running, but you never invalidate the timer, so it keeps running.

If you have your uibuttons connected, then just do myButton.enabled = YES/NO in the appropriate spot.

troop231
Apr 5, 2011, 06:05 PM
The indicator may stop running, but you never invalidate the timer, so it keeps running.

If you have your uibuttons connected, then just do myButton.enabled = YES/NO in the appropriate spot.

I may not be on the same page here, but I have them connected to the Uiwebview's default outlets, I don't actually have them coded. Edit: Screenshot: http://i53.tinypic.com/2yl0bap.png

dejo
Apr 5, 2011, 06:38 PM
...but I have them connected to the Uiwebview's default outlets...
http://i53.tinypic.com/2yl0bap.png

Actually, based on that screenshot, you have them connected to their default Received Actions. We can't tell if you have them connected to any Referencing Outlets.

troop231
Apr 5, 2011, 07:38 PM
Actually, based on that screenshot, you have them connected to their default Received Actions. We can't tell if you have them connected to any Referencing Outlets.

You are right dejo! So basically I need to declare some IBoutlets in my header file and delete the received actions connections I have already?

dejo
Apr 5, 2011, 08:30 PM
So basically I need to declare some IBoutlets in my header file and delete the received actions connections I have already?
Don't delete the Received Actions connections unless you don't want them to respond in the default way when those buttons are clicked. Do you understand the difference between an IBAction and an IBOutlet?

troop231
Apr 5, 2011, 10:22 PM
Don't delete the Received Actions connections unless you don't want them to respond in the default way when those buttons are clicked. Do you understand the difference between an IBAction and an IBOutlet?

IBAction is for methods and IBOutlet is for objects.

dejo
Apr 6, 2011, 08:41 AM
So, do you still feel you need to delete the Received Actions connections?

troop231
Apr 6, 2011, 09:15 AM
So, do you still feel you need to delete the Received Actions connections?

Nope, here's what I THINK I need to do: disable the buttons in Interface Builder, and declare a method to enable and disable forward/back buttons? Too bad I have no clue on how to accomplish it.

EDIT: Revised code: Web.h:

#import <UIKit/UIKit.h>
@interface Web : UIViewController {

UIWebView *webView;
UIButton *back;
UIButton *forward;
UIActivityIndicatorView *activityIndicator;
}

@property(nonatomic,retain)IBOutlet UIWebView *webView;
@property(nonatomic,retain)IBOutlet UIButton *back;
@property(nonatomic,retain)IBOutlet UIButton *forward;
@property(nonatomic,retain)IBOutlet UIActivityIndicatorView *activityIndicator;

-(IBAction)backButtonPressed: (id)sender;
-(IBAction)forwardButtonPressed: (id)sender;
@end and Web.m:

#import "Web.h"
@implementation Web
@synthesize webView;
@synthesize back;
@synthesize forward;
@synthesize activityIndicator;


//method for going backwards in the webpage history
-(IBAction)backButtonPressed:(id)sender{

[webView goBack];
}

//method for going forward in the webpage history
-(IBAction)forwardButtonPressed:(id)sender{

[webView goForward];
}

//programmer defined method to load the webpage
-(void)startWebViewLoad{


NSString *urlAddress = @"http://www.macrumors.com/";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webView loadRequest:requestObj];

}

// acivityIndicator is set up here
- (void)viewDidLoad {

//start an animator symbol for the webpage loading to follow
UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

//makes activity indicator disappear when it is stopped
progressWheel.hidesWhenStopped = YES;

//used to locate position of activity indicator
progressWheel.center = CGPointMake(160, 160);

self.activityIndicator = progressWheel;
[self.view addSubview: self.activityIndicator];
[self.activityIndicator startAnimating];

[progressWheel release];
[super viewDidLoad];

//call another method to do the webpage loading
[self performSelector:@selector(startWebViewLoad) withObject:nil afterDelay:0];

}


- (void)dealloc {
[webView release];
[back release];
[forward release];
[activityIndicator release];
[super dealloc];
}

#pragma mark UIWebViewDelegate methods
//only used here to enable or disable the back and forward buttons
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
back.enabled = NO;
forward.enabled = NO;
}

- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{

//stop the activity indicator when done loading
[self.activityIndicator stopAnimating];

//canGoBack and canGoForward are properties which indicate if there is
//any forward or backward history
if(thisWebView.canGoBack == YES)
{
back.enabled = YES;
back.highlighted = YES;
}
if(thisWebView.canGoForward == YES)
{
forward.enabled = YES;
forward.highlighted = YES;
}

}

@end

troop231
Apr 11, 2011, 02:24 PM
I still can't get it to work, worked a whole weekend on it to no avail.