Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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:

2ikv4m9.png


Web.h:

Code:
#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:

Code:
#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 = @"https://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

macrumors 68000
Feb 11, 2010
1,554
0
Learn to use the iOS documentation:


http://developer.apple.com/library/...ence/UIWebView_Class/Reference/Reference.html


See:

Code:
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

macrumors 603
Original poster
Jan 20, 2010
5,822
553
Learn to use the iOS documentation:


http://developer.apple.com/library/...ence/UIWebView_Class/Reference/Reference.html


See:

Code:
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

macrumors 68000
Feb 11, 2010
1,554
0
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.
 
Last edited:

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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

macrumors 68000
Feb 11, 2010
1,554
0
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

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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:
2yl0bap.png
 
Last edited:

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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.
 

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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:

Code:
#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:

Code:
#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 = @"https://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
 
Last edited:

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
I still can't get it to work, worked a whole weekend on it to no avail.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.