Reputation: 893
I am using the following code to get the name of the view controller so far navigated,
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
NSLog(@"%@",navigationArray);
but i am using this method in storyboard this is not working can any one help to fix this
Upvotes: 0
Views: 2768
Reputation: 501
This may help you to resolve issue
On Storyboard select each view controller and go to Class inspector. Then set storyboard identifier unique to each view controller.
You can access instance of any view controller.
id controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ControllerIdentifier"];
Another way
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
id controller1 = [navigationArray objectAtIndex: 0];
id controller1 = [navigationArray objectAtIndex: 1];
....
Upvotes: 0
Reputation: 5628
You will have to iterate on navigation stack and use Anbu's solution to retrieve their class name:
NSMutableArray* viewControllersNames = [[NSMutableArray alloc] init];
for(UIViewController *vc in self.navigationController.viewControllers)
{
NSString *vcName = NSStringFromClass([vc class]);
[viewControllersNames addObject:vcName];
}
In the property self.navigationController.viewControllers
, all your viewcontrollers which haven't been popped yet are stored. Here we are iterating over them and adding their class names in an array. Your first ViewController
will be at 0
index of array and your last one (The View controller which is currently presenting) will be at count-1
index.
This solution will work only if you are presenting your viewControllers via navigation controller. If you are using modal presentation, it won't work. As your post gives the impression that you're using Navigation Controller, and you say that you are doing that, so solution is also for Nav controller approach.
Upvotes: 2
Reputation: 82756
use NSStringFromClass(). It returns the name of a class as a string
.
if you want to fetch some view controller name , do like
NSString *viewName = NSStringFromClass([yourviewControllerName class]);
if you want to fetch current view controller , do like
NSString *viewName = NSStringFromClass([self class]);
fetch previous Viewcontrollers
You can use the UINavigationController
's viewControllers
property:
@property(nonatomic, copy) NSArray *viewControllers
NSArray * allViewController = self.navigationController.viewControllers;
another choice
get count of viewcontrollers available in navigation use like
NSInteger totalCount = self.navigationController.viewControllers.count;
Updated
UIViewController *allViewController = self.navigationController.viewControllers[totalCount - 2];
Class previousVCClass = [allViewController class];
NSString *className = NSStringFromClass(previousVCClass);
Upvotes: 3