1

I am in a situation in which I need to send an object and a method name a UIViewController that might or might not be instantiated in the future. To make the matter a bit more interesting, the UIViewController will be pushed to UINavigationController after two or three additional UIViewController being pushed.

What would be the best way to do this? An ugly approach would be just relay the message package (an object and a method name) via the controllers in-between. That's ugly.

Simply subclass UINavigationController and dangle the object and method name? Maybe. Then how do I know when to clear up that message pack?

NSInvocation looks really promising in this case though. Any thought?

No no no singleton, or extern struct please.


Edit: Thanks for the comments.

@Akshay, I have thought about that. Question is what if you want to send the message package (an object and a method name) to a specific object under a particular navigationController? Further, if the target object is not instantiated, you would need to clear your singleton for later use. But then how do you know when to clear up? Using singleton is just a mess.

@Praveen-K, NSNotifcation only works for those who already exist. I said I want to send a message package to a would-be-generated object.

@PragmaOnce, I and two other team mates are designing the architecture now. Hope this explains. Please, no data center singleton.

@Sedate Alien, Exactly!

@9dan, Thanks for your input. In fact, we have NSClassFromString, NSSelectorFromString, and performSelector that we can instantiate any object and send a message to execute any method with any number of objects.

The question is how to elegantly send that message across multiple objects, in my case UIViewController? Say you are at a UIViewController, and you know you would push few more UIViewControllers on top of current one. After that, you might or might not instantiate an object of a specific class and execute a method with an object. How do we do this w/o using singleton? There must be a way to handle this beautifully. I think NSInvocation has that capacity.

Stkim1
  • 1,365
  • 2
  • 9
  • 10
  • Why don't you save the stuff in some ivar (of say, a singleton class) and let it be accessed by the `UIViewController` when it gets instantiated? – Akshay Sep 27 '11 at 03:44
  • You can use the NSNotification and notify to receiver on your changes. – Praveen-K Sep 27 '11 at 03:48
  • 2
    To be honest, problems like this arise when you haven't planned he flow/architecture of your software. I suggest you have some sort of "Data Center" singleton. – 0xSina Sep 27 '11 at 04:19
  • 1
    @PragmaOnce: _Introducing_ a singleton to improve software architecture sounds strange. – Aidan Steele Sep 27 '11 at 05:17
  • I really don't understand OP and Alien's hatred of singleton. If it's from 90's, it's bad? – 0xSina Sep 27 '11 at 05:31
  • I just want something elegant. There are tons of stuff for message passing in Obj-C and there must be good one. And yet we stick to singleton? Well, I'd rather use extern. It's fast, clear, no obj-c overhead whatsoever. I lay my eyes to NSInvocation now. – Stkim1 Sep 27 '11 at 05:52
  • You can safely send any message to nil object, you can make a SEL type object from any method, you can use `performSelector` call, etc. It's trivial work in the Objective-C world, only hard for C/C++. – 9dan Sep 27 '11 at 07:34
  • Create a notification center, store the message, let the controller fire a notification when it is created, and let the class receiving the notification send the message. The class handling the messages can be an ivar in your delegate. You can answer comments in this comments section. I think the question would benefit from an extended explanation of your architecture. – Jano Sep 27 '11 at 08:21

0 Answers0