'Using UINavigationController to decide which view is currently showing

I would like to know if there is a way to tell which UIViewController is currently showing by using the UINavigationController.

I have a delegate in which I return some information back to a specific UIViewController however now I am wanting to take that same data and return it to a different UIViewController depending on an if statement, i.e. which UIViewController called the specific request method I have, So in turn I can return the data back to the right UIViewController.



Solution 1:[1]

in general if you want an object to store/return information that it doesn't usually store based on its existing properties instance variables, you can use objective-c's runtime object association.

So consider this category:

#import "NSObject+Addons.h"
#import <objc/runtime.h>    

@implementation NSObject (Addons)

static char infoKey;

-(id)info {
    return objc_getAssociatedObject(self, &infoKey);
}

-(void)setInfo:(id)info {
    objc_setAssociatedObject(self, &infoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

now suppose that we are in viewController1 and it makes a specific request.. so you can put this code

//viewController1.m

// make specific request
// now store in UINavBar name of this viewcontroller
// (you can use an enum here instead if you like)
[self.navigationController setInfo@"viewController1"];    

so later on in your code, when you want to return the data back to the return controller that made that specific request (you didn't say exactly where).. you can check the value of info:

if ([[navigationController info] isEqualToString:@"viewController1"]]) {
   //do stuff 
} else if ([[navigationController info] isEqualToString:@"viewController2"]]) {
   // etc..

this is the quick solution.. however if it was my own code.. I would use the mediator pattern.. basically create a controller of controllers (let's just call it controller), and make it a singelton. This object would keep the in-between info such as which viewcontroller did what, and return that information whenever necessary.. in a way the UINavigationBar is doing this in the code above, but we're using runtime patchwork code to make it happen. Using the controller of controllers is more robust and the concept can cover many other useful tasks that come up for most projects. The idea is design code that is closed for modification but open for extension.

Solution 2:[2]

you can get the information about all UIViewController's pushed in UINavigationController.

Try this.

NSArray *viewCtrls = self.navigationController.viewControllers;

// for Current showing viewController
UIViewController *vCtrl = [viewCtrl objectAtIndex:[viewCtrl count]-1];
if([vCtrl isKindOfClass:[<YourControllerClass> class]]) 
{
 //Your code
} 

Solution 3:[3]

Please note that these solutions will work only if the view controller your are looking for exists in navigation stack of the same navigation controller.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abbood
Solution 2
Solution 3 sunny