Get class name of Parent View controller in iPhone

2019-04-06 06:06发布

问题:

I'm pushing a view like this.

[[self.navigationController pushViewController:myViewController animated:YES];

How Can I get class name of parent view controller from pushed view (child view)? Any Idea?

回答1:

This should do it:

NSUInteger numberOfViewControllersOnStack = [self.navigationController.viewControllers count];
UIViewController *parentViewController = self.navigationController.viewControllers[numberOfViewControllersOnStack - 2];
Class parentVCClass = [parentViewController class];
NSString *className = NSStringFromClass(parentVCClass);


回答2:

Get class of parent view controller:

id class = [self.navigationController.viewControllers[0] class];

and its name:

NSString *className = NSStringFromClass(class);