2014-12-03 128 views
0

我已經創建了一個UIViewController與UIWebView並試圖添加爲另一個UIViewController的子視圖。這會導致崩潰,從而發送錯誤的請求錯誤。iphone sdk:UIWebView崩潰

包含的WebView代碼控制器是如下:實現WebViewController是

@interface WebViewController : UIViewController 

@end 


@interface WebViewController() <UIWebViewDelegate> 
{ 


    IBOutlet UIWebView *webView; 
} 
@end 

@implementation WebViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    webView.delegate = self; 
    NSString *str = [NSString stringWithFormat:@"<html><body>This is html text</body></html>"]; 
    [webView loadHTMLString:str baseURL:nil]; 
    // Do any additional setup after loading the view. 
} 

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

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

    return YES; 
} 
- (void)webViewDidStartLoad:(UIWebView *)webView{ 

} 
- (void)webViewDidFinishLoad:(UIWebView *)webView{ 

} 
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ 


} 

代碼:

WebViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"webView"]; 
    [self.view addSubview:obj.view]; 
+0

你必須保持強大的refrence爲WebViewController * OBJ所以它不會被釋放,例如[自addChildViewController:OBJ]。或者讓一些資產在某個地方上漲 – 2014-12-03 12:57:18

回答

1

您需要添加,控制器之間的關係,補充一點:

 [self addChildViewController:obj]; 

在你的代碼是:

WebViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"webView"]; 
    [self addChildViewController:obj]; 
    [self.view addSubview:obj.view]; 
+0

非常感謝!正常工作 – 2014-12-03 12:57:41

0

UIViewController實例不應該被用作簡單initialisers及其view S的代表,通常使用的UIWindow,UIViewController中的一個(或它的子類)的方法來呈現另一視圖控制器,在使用你的情況的WebViewController實例作爲child view controller似乎是合理的:

[self addChildViewController:obj]; 
[self.view addSubview:obj.view]; 
+0

是的,它工作,謝謝。 – 2014-12-03 14:59:30