2016-05-30 32 views
0

嘗試阻止網頁瀏覽反彈,當用戶拖動應用程序/屏幕時,頂部會出現一個空白區域。這是不正常的代碼:停止網頁瀏覽反彈顯示錯誤

#import "Webview.h" 

@interface Webview() 

@end 

@implementation Webview 
@synthesize webView; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    NSString *urlAddress= [[NSBundle mainBundle] pathForResource:@"demos/index" ofType:@"html"]; 
    NSURL *url = [NSURL fileURLWithPath: urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [webView loadRequest:requestObj]; 

    webView.scrollView.delegate = self; 

    webView.scrollView.bounces = NO; 
    } 




- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    //The webview is is scrolling 
    int yPosition = [[webView stringByEvaluatingJavaScriptFromString: @"scrollY"] intValue]; 

    if([[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue] - yPosition == webView.frame.size.height) 
    { 
     //The user scrolled to the bottom of the webview 
     webView.scrollView.bounces = YES; 
    }else if([[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue] - yPosition > webView.frame.size.height + 100){ 
     webView.scrollView.bounces = NO; 
    } 

} 

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{ 
    NSLog(@"Finish"); 
} 



/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end 

有消息:分配到「身份證_Nullable」從omcompatible型「web視圖* const的__strong」

回答

0

如果錯誤就行了:

_webView.scrollView.delegate = self; 

...然後錯誤是你的webView實例不是正確的類型。

  1. 檢查您的webView實例是否爲UIWebView的子類。
  2. 你應該採用UIScrollViewDelegate協議,通過包括用於所述接口的協議聲明:@interface ViewController : UIViewController <UIScrollViewDelegate>
+0

我應該把'@interface的ViewController:UIViewController中'代替'@interface網頁視圖()'? –

+0

只要你採用'UIScrollViewDelegate',你應該沒問題。如果你仍然得到這個錯誤,那麼在你可以訪問該類的地方設置一個斷點,並在控制檯中打印出類的類,使用'po [var_name class]' – Sheamus