2013-03-20 26 views
4

我已经在我的应用程序中使用UIWebView实现了浏览器,默认情况下我正在浏览器中加载谷歌页面。在iPad中的UIWebView后退按钮实现问题

当我在谷歌页面搜索某些东西时,调用UIWebViewDelegatewebView:shouldStartLoadWithRequest:navigationType:方法。

问题是,当我点击从这个搜索页面的后退按钮没有代表被调用,所以我有一个问题,禁用我的后退按钮。

此问题仅在iPad应用程序不在iPhone应用程序中发生。

回答

5

此代码可以帮助ü...

一个UIWebView在用户的应用程序的剩余,而可以加载一个网页一个UIView。 通过在网页中使用嵌入式链接,可以导航到其他网页。通过历史记录的前进和后退导航可以使用goForward和goBack实例方法进行设置,但程序员必须提供按钮。

下面的示例使用一个UIWebView,和

1)增加了向前和向后按钮。的按钮被激活,并使用高亮UIWebViewDelegate可选方法webViewDidStartLoad:和webViewDidFinishLoad:

2)增加了,而加载网页时

在用于WebViewController .h文件,其显示UIActivityIndi​​catorView:

声明UIWebView,可选:添加按钮来控制向前和向后移动浏览历史和IBActions按下按钮,可选地再次:添加一个UIActivityIndi​​catorView。

@interface WebViewController : UIViewController <UIWebViewDelegate> 
{ 
    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 

//在用于WebViewController .m文件

@implementation WebViewController 

@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.google.com"; 
    NSString *urlAddress = @"http://cagt.bu.edu/page/IPhone-summer2010-wiki_problemsandsolutions"; 

    //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 

/* ** * ** * ** * ** * ** * ** * ** * ** * ****/

//In viewDidLoad for the class which adds the WebViewController: 


WebViewController *ourWebVC = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil]; 

ourWebVC.title = @"WebView"; 
[self.view addSubview:ourWebVC]; 

//release ourWebVC somewhere else 
4

在你的情况,你必须忽略/避免 “缓存数据”。以下几行代码可能会有所帮助。

NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]; 
[webView loadRequest:requestObj];