2013-03-20 54 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];