Question

I'm having trouble with getting the -dismissModalViewControllerAnimated: to work on the iPad (as an iPhone app). For some reason it doesn't seem to do anything.

I call -presentModalViewController:animated: in MainViewController, after which I've tried calling the -dismissModalViewController from the presented view controller (using [self dismissModalViewController]), which I understand forwards the request to the MainViewController. I've also tried setting a delegate in the presented view controller (viewControllerAboutToBePresented.delegate = self;), and then calling [self.delegate dismissModalViewController:YES]. Neither approach seems to do anything when I run the iPhone app on the iPad.

How can I dismiss the modal view controller on the iPad?

Was it helpful?

Solution

I had this the first time I ported an iPhone project to the iPad - it was [self dismissModalViewControllerAnimated:] that was quietly failing. The project was using Cocoa view controllers over an OpenGL background, and I thought it was something to do with that.

Because of the ridiculously tight deadline, I had no time to work out what was going on, and so I just added the modal view as a subview of the current view controller, and removed it when I was done. (Yeah, a hack, but that's timescales for ya...)

OTHER TIPS

I woud post this as a comment, but I'm unable to do so.

Is the main view controller that should call dismissModelViewControllerAnimated:. You can either call [[self parentViewController]dismissModalViewControllerAnimated:] in the presented view controller or define a method in a protocol that dismiss the modal view controller and implement the protocol in the main view controller, set it as the delegate of the presented view controller and call the method from it. You are doing it the wrong way. It might or might not solve your issue.

Update (code sample isn't available on comments):

On the MainViewController you should have something like this

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // In this case is inside a tableviewmethod, but it could really be an action associated with a button.

    DetailViewController *controller = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    [controller setDelegate:self]; // The delegate is the parent and is assigned, not retained.

    // Modal presentation style is only used on iPad. On iPhone is always full screen.
    // [controller setModalPresentationStyle:UIModalPresentationFullScreen];

    [controller setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:controller animated:YES];
    [controller release]; // It will be deallocated upon dismissal.
}

-(void)dismissDetailViewControllerAndProcessData:(NSDictionary *)data {

    // Do something with the data passed.
    [self processData:data];

    // Dismiss the modalviewcontroller associated with this viewcontroller.
    [self dismissModalViewControllerAnimated:YES];
}

while on the detail view controller presented as modal view controller, the only thing needed is something like this

-(void)actionBack:(id)sender {

    // Call the delegate method. If you just need to dimiss the controller, just
    // call
    // [[self parentViewController]dismissModalViewControllerAnimated:YES];
    // and don't even bother to set up a delegate and a delegate method.

    [delegate dismissDetailViewControllerAndProcessData:nil]; // Call the parent dismissal method.
}

but if the application is running fine on iPhone, it should run just as fine on the iPad as iPhone app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top