dinesh
dinesh

Reputation: 837

App crashes while Modal view controller closes

I want to display the modalViewController from the 2nd screen of the app, and when I dismiss the controller it should navigate to the 1st screen. The below code works fine in iPhone 4, 5 and iPod Touch but NOT in iPAD. The objective is when I dismiss the modalViewController it shouldn't go back to the second screen, but it should display the first screen.

        ShareEmail *shareEmail = [[ShareEmail alloc] initWithNibName:[NSString stringWithFormat:@"%@",xibShareEmail] bundle:nil];

        shareEmail.fromWer = @"ownPer";
        [self presentModalViewController:shareEmail animated:NO];
        [shareEmail release];

        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

In share email class

[self dismissModalViewControllerAnimated:YES];

Upvotes: 0

Views: 160

Answers (2)

Daddy
Daddy

Reputation: 9035

Something interesting is happening. You are presenting a view controller from self, and then you're having the navigationController perform its backwards navigation. At this point, the self I was talking about earlier disappears.

Remove the popViewController method from your presentViewController method.

Also, you'll need to use the ^completion handler method. That's where you should put your navigation controller pop code.

-(void)present {
    ShareEmail *email = [[ShareEmail...
    //You'll need to get a weak reference to `self` in `email`
    email.modalDelegate = self; //you need to make a property in ShareEmail
    [self presentViewController:email animated:YES completion:nil];
    [email release];
}

-(void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

//in ShareEmail.m
[modalDelegate dismiss];

Upvotes: 0

Jeff Loughlin
Jeff Loughlin

Reputation: 4174

You need to retain your shareEmail view controller - make a synthesized retained property

YourClass.h:

@property (nonatomic, retain) ShareEmail *shareEmailViewController;

YourClass.m:

@synthesize shareEmailViewController;

Then display your modal view controller by:

ShareEmail *shareEmail = [[ShareEmail alloc] initWithNibName:[NSString stringWithFormat:@"%@",xibShareEmail] bundle:nil];
self.shareEmailViewController = shareEmail;
[self presentModalViewController:shareEmail animated:NO];
[shareEmail release];

The retained property will keep the view controller from being deallocated while in use. Then when you dismiss it later you can do:

[self.shareEmailViewController dismissModalViewControllerAnimated:YES];
self.shareEmailViewController = nil;

which will release the retained property and free the memory after you're done with it.

Upvotes: 1

Related Questions