Hopefully this is an easy one…
I have an NSObject with methods that is used in multiple UIViewControllers (the NSObject is imported in my .pch file).
The UIViewControllers then make calls to the NSObject like so;
[ThisNSObject doSomething];
This is all working to plan so there’s no issue there… however, I would love the method doSomething to be able to detect WHICH UIViewController made the call to that NSObject. Then, based on that information I can manipulate the UIViewController in any given way.
The reason I need this is because if I have a UITabBar with each Tab loading a different UIViewController, but all making a call to the global NSObject, I need to direct further action to that one specific UIViewController.
I know I have access to keyWindow, but I’m not sure this is exactly what I’m after.
Any suggestions would be great, thank you.
Roy
EDIT:
Actually, perhaps in the NSObject I can detect which tab is currently selected, then get the top View in the stack… and make a reference like that? Does anyone have thoughts on why this would be a bad idea?
Using runtime code you could probably drill back through the call stack, but it’s a rather complex solution to a simple problem. I would suggest that you look to the APIs for inspiration and modify your doSomething method to take a controller argument like this:
This way when you can the doSomething method you pass a reference to self like this:
And your problem is solved – simply.
P.S. if you have other parameters, an alternative signature style might be
It all depends on what makes the best sense to you.