UINavigationcontroller popViewcontroller does not work at second try

2k views Asked by At

I encountered a little problem with my app. It is a simple problem but i can not find out what is the cause of crash.

I have a simple viewcontroller with only a webview (created and linked in storyboard) (ARC enabled):

@implementation BPActivateController
@synthesize mainWebView;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/test.php?code=%@", BASE_URL, [[NSUserDefaults standardUserDefaults] objectForKey:@"uniqueIdentifier"]]]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [mainWebView loadRequest:requestObj];

    [super viewDidLoad];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
    if ([html rangeOfString:@"<h1>Not Found</h1>"].location != NSNotFound)
    {
        [self.navigationController popViewControllerAnimated:YES];
        NSLog(@"pop the view controller");
    }
}

- (void)viewDidUnload
{
    [self setMainWebView:nil];
    [super viewDidUnload];
}

The first time the view opens the popViewControllerAnimated is called and the user is redirected back to the last viewcontroller. But when the view is opened again, gives me a EXEC_BAD_ACCESS after the popViewControllerAnimated is called. When i enable the Zombie code gives me the following trace:

2012-06-04 11:51:12.717 actusmedicus[410:707] pop the view controller
2012-06-04 11:51:12.720 actusmedicus[410:707] *** -[BPActivateController respondsToSelector:]: message sent to deallocated instance 0xc6d95b0

I have tried several things but still is is not clear what selector is being called. I suspect that the webview is still busy..

Anyone has an idea that gets me back on track?


[EDIT]

The EXEC_BAD_ACCESS is fixed by setting the delegate of the webview to nil. But the real problem is still there, the first time the view is pushed on the UINavigation stack, the web view does load and after a 404 the popViewControllerAnimated is successfully executed. But the second time we push the same view on the UINavigation stack (the same way we did the first time) The popViewControllerAnimated does not do anything. I have checked if it is nil but that does not seem to be the problem..

My first idea was that it runs in another thread and the UINavigationcontroller does not exist there. I ruled out that possibility with the help of performSelectorOnMainThread.

Finally i added a button to call popViewControllerAnimated manually and that works every time, so why doesn't it work when i call it programmatically?

2

There are 2 answers

2
Piepants On BEST ANSWER

I suspect that the webview is still busy..

If so then in your dealloc call the stopLoading method.

You should also set the delegate to nil in the dealloc.

0
Zafer Fatih Koyuncu On

Make sure if you are not segueing twice to the destination view controller. In my case, my code fired the segue twice. So I had to click back button twice to come back to my main controller.