2014-12-03 138 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