简体   繁体   中英

presentingViewController getting always UITabBarController

I just add TabBarController + NavigationController. Previous to this everything was ok but now when I call presentingViewController from a modal, I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController tableViewListado]: unrecognized selector sent to instance

Instead of receiving the expected object (ViewController) I'm getting "UITabBarController", should I get the presenting controller in some different way when using TabBar and Nav controllers?

Without the TabBar/Nav I was using this:

ViewController *parentView = (ViewController *)[self presentingViewController]; 

[parentView something];

Edit:

Just find out that if I do this it works, but dont think this is actually the best way to do it:

ViewController *parentView = (ViewController *)[(UINavigationController *)[((UITabBarController *)[self presentingViewController] ) selectedViewController] topViewController]  ;

[parentView something];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM