I currently have made an app and I want to add a UIToolbar to control a UIWebView.
Currently I am having these issues:
1) The buttons are not interacting with the webview even though i connected everything in interface builder
2) my stopLoading and refresh buttons are not showing up bordered as I made them in interface builder.
3) my webview is sliding underneath my toolbar instead of opening up below it.
Here is my code:
Currently I am having these issues:
1) The buttons are not interacting with the webview even though i connected everything in interface builder
2) my stopLoading and refresh buttons are not showing up bordered as I made them in interface builder.
3) my webview is sliding underneath my toolbar instead of opening up below it.
Here is my code:
Code:
// WebViewController.h
// TheSalesHuddle
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *myWebView;
UIActivityIndicatorView *spinnerView;
UIToolbar *toolbar;
}
@property (nonatomic, retain) IBOutlet UIWebView *myWebView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *spinnerView;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@end
Code:
// WebViewController.m
// TheSalesHuddle
//
// Created by Roy Jossfolk Jr on 2/24/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "WebViewController.h"
@implementation WebViewController
@synthesize myWebView;
@synthesize spinnerView;
@synthesize toolbar;
- (void)loadWebPageWithString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
//Tab Bar Image and title
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
if ((self = [super initWithNibName:nibName bundle:bundle]))
{
UIImage *tabImage = [UIImage imageNamed:@"blogIcon.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc]
initWithTitle:@"BLOG"
image:tabImage
tag:0];
self.tabBarItem = tabBarItem;
[tabBarItem release];
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[myWebView stopLoading];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://thesaleshuddle.com"]]];
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlack;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 0, 320, 44);
// add buttons
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self action:@selector(action)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(action:)];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]
initWithTitle:@"Back" style:UIBarButtonItemStyleBordered
target:self action:@selector(action)];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc]
initWithTitle:@"Forward" style:UIBarButtonItemStyleBordered
target:self action:@selector(action)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: item1, item2, flexItem, item3, item4, nil];
//release buttons
[item1 release];
[item2 release];
[item3 release];
[item4 release];
[flexItem release];
//add array of buttons to toolbar
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// release
- (void)dealloc {
[myWebView release];
[spinnerView release];
[super dealloc];
}
#pragma mark -
#pragma mark UIWebViewDelegate methods
- (void)webViewDidStartLoad:(UIWebView *)myWebView
{
[spinnerView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)myWebView
{
[spinnerView stopAnimating];
}
@end