0

I'm using a UIWebView like that in the viewDidLoad:

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 88, 320, 400)]; //  init and create the UIWebView
    webView.autoresizesSubviews = YES;
    webView.userInteractionEnabled = NO;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [webView  setDelegate:self];//set the web view delegates for the web view to be itself
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]];//Create a URL object & Set the URL to go to for your UIWebView
    [webView loadRequest:requestObject];//load the URL into the web view.
    [self.view addSubview:webView];//add the web view to the content view
    [webView release], webView = nil;

When I try to change the page, I try that BUT it doesn't working :

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
[webView reload];

The code that's just above is in the action of a TabBar button.

**EDIT : When I comment

[webView release], webView = nil;

I can see the apple website but not facebook when I push the button! The screen is refreshing in the apple website and not facebook...**

Hundred of thanks to help ;-)

1
  • You don't need to do [webView reload]; after [webView loadRequest:..];. Commented Jul 1, 2011 at 9:39

5 Answers 5

4

You've released and set your webView to nil. So the object no longer exists. You are calling loadRequest and reload on a nil object which will do nothing.

Assuming you have webView as an instance variable in your class, remove the line

[webView release], webView = nil;

and instead put that in your dealloc method otherwise when you call

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];

then you will be calling it on a nil object which will do nothing.

Sign up to request clarification or add additional context in comments.

3 Comments

OK, it's working perfectly now ! I have delete the two lines and put yours and all is all right, THANKS !!!!
@clement Don't forget you will still need to call release on the webView otherwise you will leak memory. Put it in your dealloc method
Yes thanks, I've put "[webView release]" in the dealloc method
2

Where did you try this code ?

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
[webView reload];

You already released webview and make it Nil. So it will not work if you did it after it loads www.apple.com.

[webView release], webView = nil;

Remove above two lines once and then try. It should work. Hope this help.

Comments

1

The potential problem I see is that you are setting the webView value to nil after adding it as subview, but before doing the reload:

 [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
 [webView reload];

here webView is nil... the object still exists (because it is embedded in a view), but the variable does not point to it anymore, so messages are simply ignored.

Comments

1

no need of reload

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
     [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
}

this may help..!!

Comments

0

Reading your code, I guess that the your webView is declared as @property in the header file. You release this object after the Apple site is loaded. I think that's the reason why it doesn't load the facebook website.

The [webView release]; should be placed within the - (void) dealloc method only.

Please let me know if it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.