2016-08-05 73 views
3

後,我已經開發了方法viewDidLoad中的web視圖中的ViewController崩潰設定的WebView委託

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
id <UIWebViewDelegate> delegate =[[MyDelegate alloc] init]; 
webView.delegate = delegate; 
NSError *error; 
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 

NSString *htmlContent = [[NSString alloc] initWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error]; 
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 

[self.view addSubview:webView]; 

我設置委託階級MyDelegate的實例。

在MyDelegate類:

#import <UIKit/UIKit.h> 

@interface MyDelegate : NSObject <UIWebViewDelegate> 

@end 



#import "MyDelegate.h" 

@implementation MyDelegate 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

    return YES; 

} 
@end 

但App我崩潰期間啓動loasding。 如果我沒有加載html內容,但會發生url(例如'google.com')崩潰。

當我評論這個'webView.delegate = delegate;'崩潰不會發生。

我知道我可以在ViewController.h使用:

@interface ViewController : UIViewController<UIWebViewDelegate> 

,這在viewDidLoad中:

webView.delegate = self; 

但我需要使用其他類爲代表(不視圖控制器),但網頁視圖必須位於ViewController中。

我該如何做到這一點? 幫幫我!

+0

其他類爲代表,你可以使用協議。 –

+0

感謝您的評論,但你是什麼意思?我使用協議UIWebViewDelegate。我需要使用其他協議嗎? – Katya

+0

什麼是崩潰消息? –

回答

0
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
    id<UIWebView> delegate =(id)self; 
    webView.delegate = delegate; 
    NSError *error; 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 

    NSString *htmlContent= [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error]; 
    [webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 

    [self.view addSubview:webView]; 
} 

MyDelegate.h

#import <UIKit/UIKit.h> 

@protocol UIWebView <UIWebViewDelegate> 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; 


@end 
@interface MyDelegate : NSObject 
@end 
0

在您的視圖控制器本身可以實現UIWebViewDelegate。

- (無效)viewDidLoad中 {

[super viewDidLoad]; 

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
webView.delegate = self; 

NSError *error; 

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 

NSString *htmlContent= [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error]; 
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 

[self.view addSubview:webView]; 

}

- (BOOL)web視圖:(的UIWebView *)webView的shouldStartLoadWithRequest:(的NSURLRequest *)請求navigationType:(UIWebViewNavigationType)navigationType {

return YES; 

}

1

讓我指出根本原因。 UIWebView委託屬性是一個弱引用(Swift源代碼中的「unowned(unsafe)」),這意味着它的內存可以在任何時候被釋放。

所以要解決這個問題,你必須保留一個對你的控制器的引用作爲類屬性。溶液

實例斯威夫特成功測試:

class MyUIViewController : UIViewController{ 
    let leftDelegate:MyWebViewDelegate = MyWebViewDelegate() 
    ... 
} 
0

我發佈一個單獨的答案,因爲我認爲有需要提供明確的Objective-C的解決方案,因爲OP可能會錯過大衛·岡薩雷斯的答案,因爲它是在斯威夫特,雖然他絕對釘上了它。

問題是,當web視圖調用它的委託時,該委託已被釋放。因此,解決辦法是MyDelegate類型添加屬性ViewController私有擴展聲明:

@interface ViewController() 
@property (nonatomic, strong) MyDelegate* webViewDelegate; 
@end 

然後存儲在-viewDidLoad在屬性創建MyDelegate例如:

... 
id <UIWebViewDelegate> delegate =[[MyDelegate alloc] init]; 
webView.delegate = delegate; 
self. webViewDelegate = delegate; 
...