Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong 'presentingViewController'

In my MainViewController, I present another view controller through this:

MessageViewController *messageController = [[MessageViewController alloc]initWithNibName:nil bundle:nil];

[messageController setModalPresentationStyle:UIModalPresentationFullScreen];
[messageController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:messageController animated:YES completion:nil];

[messageController release];

This will display the view controller correctly. However, when I try to go back to the presenting view controller, which in this case should be the MainViewController, this code doesn't work:

if ([self.presentingViewController isKindOfClass:[MainViewController class]])
    [(MainViewController *)self.presentingViewController setCurrentViewTag:2];

[self dismissModalViewControllerAnimated:YES];

I removed the "if.." condition to force it in setting the current view tag. An error occurred telling me that the presenting view controller seems to be the UINavigationController:

[UINavigationController setCurrentViewTag:]: unrecognized selector sent to instance 0x8352a50

Can anyone tell me why is this happening? This code used to work before and I am not sure what changed to make it stop working properly.

EDIT

Here is the updated code:

ReaderController *readerController = [[ReaderController alloc]initWithNibName:nil bundle:nil];
[readerController loadWhichViewToShow:2];

[self setDefinesPresentationContext:YES];

[readerController setModalPresentationStyle:UIModalPresentationFullScreen];
[readerController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:readerController animated:YES completion:nil];

[readerController release];
like image 769
Anna Fortuna Avatar asked Oct 29 '12 09:10

Anna Fortuna


3 Answers

Calling [self presentViewController:messageController animated:YES completion:nil]; doesn't necessarily use the vc you call this on to present the other vc. By default it travels up the vc-hierarchy and presents the other vc on the root view controller. That's why in your case the presenting view controller is a UINavigationController.

If you want to force your MainViewController to be the presenting vc, you have call:

[self setDefinesPresentationContext:YES];

on your MainViewController before presenting the MessageViewController.

Edit: In case someone else reads this: definesPresentationContext seems to be bugged or the documentation is wrong. See the comments below and Cocoa Builder

like image 66
Tobi Avatar answered Oct 05 '22 06:10

Tobi


copy of my answer from this question

from Programming iOS 6, by Matt Neuburg:

On the iPad, when the presented view controller’s modalPresentationStyle is UIModalPresentationCurrentContext, a decision has to be made as to what view controller should be the presented view controller’s presentingViewController. This will determine what view will be replaced by the presented view controller’s view. This decision involves another UIViewController property, definesPresentationContext (a BOOL). Starting with the view controller to which presentViewController:animated:completion: was sent, we walk up the chain of parent view controllers, looking for one whose definesPresentationContext property is YES. If we find one, that’s the one; it will be the presentingViewController, and its view will be replaced by the presented view controller’s view. If we don’t find one, things work as if the presented view controller’s modalPresentationStyle had been UIModalPresentationFullScreen.

TL;DR
1. set definesPresentationContext to true on the desired presentingViewController
2. set modalPresentationStyle to UIModalPresentationCurrentContext on the desired presentedViewController

like image 24
calql8edkos Avatar answered Oct 05 '22 06:10

calql8edkos


If seems that you need to set three thing in iOS 11.

controller.modalPresentationStyle = UIModalPresentationCurrentContext;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
self.definesPresentationContext = YES;
[self presentViewController:controller animated:YES completion:nil];
like image 40
code4j Avatar answered Oct 05 '22 07:10

code4j