views:

81

answers:

2

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?

A: 

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.

sigsegv
Thanks for your reply, sigsegv. I could be wrong, but I'm pretty sure that [self parentViewController] is going to be the object that calls -presentModalViewController, which I called by using self.delegate. In any event, I also tried it with [self parentViewController] as you described. Have you had luck with this message on the iPad in an iPhone app? (I haven't used modal view controllers in apps designed for the iPad yet)
JoBu1324
I don't remember of any problem about modal view controller when running iPhone app on iPad. I don't have an iPad at hand, but the code updated in the post above works fine in an iPhone only app running in the iPad simulator.
sigsegv
A: 

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...)

Echelon
The main reason I was interested in the modal view controller was because I could apply transitions to the presentation effect - otherwise the navigation controller would have been the easier way to go. As it stands, unless someone posts an answer I'll be doing something along the lines of your solution: I'll change it back to use the navigation controller.
JoBu1324
I ended up going this route for now...
JoBu1324