Reputation: 178
I have created one UIViewController with UIWebView
and trying to add as subview to another UIViewController. That causes to crash which sends bad request error.
The code Controller that contains WebView is as follow:
@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{
}
Code that implements the WebViewController is :
WebViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"webView"];
[self.view addSubview:obj.view];
Upvotes: 0
Views: 120
Reputation: 8944
UIViewController
instances are not supposed to be used as simple initialisers and delegates of their view
s, normally you use one of the UIWindow, UIViewController (or it subclass) methods to present another view controller, in your case using an instance of WebViewController
as a child view controller seems reasonable:
[self addChildViewController:obj];
[self.view addSubview:obj.view];
Upvotes: 0
Reputation: 5047
You need to add, the relationship between controller, add this:
[self addChildViewController:obj];
In your code is:
WebViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"webView"];
[self addChildViewController:obj];
[self.view addSubview:obj.view];
Upvotes: 1