Pankti Patel
Pankti Patel

Reputation: 178

iphone sdk : UIWebView crash

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

Answers (2)

A-Live
A-Live

Reputation: 8944

UIViewController instances are not supposed to be used as simple initialisers and delegates of their views, 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

Onik IV
Onik IV

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

Related Questions