It looks like you're new here. If you want to get involved, click one of these buttons!
//ViewController1
@interface ViewController1 : UIViewController <UIWebViewDelegate>
@property (nonatomic, retain) IBOutlet UIWebView* testWebView;
@end
@implementation ViewController1
@synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
[testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@\"https://www.google.com\"]]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *html = [testWebView stringByEvaluatingJavaScriptFromString:
@\"document.body.innerHTML\"];
if ([self.testWebView.request.url.absoluteString rangeOfString:@\"output=search\"].location != NSNotFound) {
ViewController2* newViewController = [[ViewController2 alloc] init];
[newViewController setTestWebView:self.testWebView];
[self.navigationController pushViewController:newViewController animated:YES];
}
}
- (void)dealloc{
[self.testWebView stopLoading];
self.testWebView.delegate = nil;
self.testWebView = nil;
}
@interface ViewController2 : UIViewController <UIWebViewDelegate>
@property (nonatomic, retain) UIWebView* testWebView;
@end
@implementation ViewController2
@synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.testWebView.delegate = self;
[self.view addSubview:testWebView];
[self performSelector:@selector(loadDifferentPage) withObject:nil afterDelay:10];
}
-(void)loadDifferentPage{
[self.testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@\"http://www.stackoverflow.com/\"]]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@\"%s\", __PRETTY_FUNCTION__);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@\"%s\", __PRETTY_FUNCTION__);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@\"%s\", __PRETTY_FUNCTION__);
return YES;
}
Replies
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThe web view being retained and the calling of the delegate methods are not in any way related. So saying that one happened but the other didn't is really a nonsense statement. I do have air in my tires, but my engine won't start. Those two things do not relate.
Plus, if you're using ARC, the properties should be strong, not retain.
Passing the view around like this is a really bad idea. Although it shouldn't happen within a navigation controller, if your VC1 gets deallocated, the web view's delegate will be set to nil even though it might currently be sitting in VC2.
I have personally found web views to be extremely finicky if they are not currently on screen. Add some logging to find out what the status of the web view and it's delegate are in that viewDidLoad method. Although I do not see a reason why it shouldn't work from a technical standpoint, it's possible that something is getting cleared out.
But I'll reiterate that this is a really bad design.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeEven if VC1 gets deallocated the webview should have been retained or still should have a strong pointer to the webview from VC2. Which is working correctly.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeLike I said, it's unlikely that VC1 will be deallocated before VC2 in this case, but I highlight this as an example of why this is a really bad approach.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeFound out the issue. The issue is as I am setting delegate to nil in dealloc of VC1 which is happening after viewdidload of VC2 is getting called. The delegate is being set to nil once VC1 is getting deallocated. If I add the delegate for the webview in loadDifferentPage method it will work fine. But this isn't the correct way of doing it. So, I need to figure out how I have to handle this scenario. I could do
I will test this now.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAnd I'm sitting here telling you that your whole approach is wrong, but you decide to care about where you are setting the delegate?
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeActually the client didn't want me to reload the page again. That is the reason why I had to take this approach. Could please tell me how you would approach this scenario?
Sorry Brian, but just to let others know after I updated dealloc its working fine. But as Brian said we will be having issues with this approach.., if for some reason if the delegate of the webview is not set to nil it will crash your application.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome