Notice a view is loaded from outside of a viewController

Go To StackoverFlow.com

0

Any help would be appreciated.

I am looking for a way to programatically notified that a view is loaded from outside of that viewController.

Lets say my main view has 5 buttons, after the view is loaded and the buttons appeared I want to be notified in another file (outside of that viewContrller) that it is loaded. How/Where can I check this and be notified?

Do I need to do some Aspect Oriented Programming?

2012-04-04 20:41
by ramo
Is this possible to not add/change anything in the source code of that viewcontroller. All the code changes I want to made to track should be coded outside of that viewController in the control file - ramo 2012-04-04 20:52
You can make your control object an observer of a property change inside another object. I've updated me answer with an explanation - Phillip Mills 2012-04-04 21:19


2

Use NSNotificationCenter. You can communicate between classes.

2012-04-04 20:43
by Eric


0

Listen for a custom NSNotification inside your other object. Have your view controller post that notification during whichever part of its life cycle makes most sense (viewDidLoad, viewDidAppear ...).

If you can't post a notification, then observing a keyPath might be the way to go. For example, you can put something like this in your control object and then implement observeValueForKeyPath::

[viewController addObserver:self
                 forKeyPath:@"view"
                    options:NSKeyValueObservingOptionNew
                    context:NULL];
2012-04-04 20:44
by Phillip Mills


0

NSNotificationCenter or a delegate method is the most appropriate way to accomplish this.

2012-04-04 21:00
by Sam


0

While you can do this with notifications as others have suggested or KVO, this strongly suggests a design problem. You should never be accessing a view controller's internal views directly. So the deeper question is: why do you want to know?

The most likely cause in my experience is that you're letting some other object set the titles or modify enabled. This breaks MVC and leads to the kind of problems you're probably trying to fix. The correct way to handle this is to put the data into a model object that is shared between the various view controllers. The current view controller can then observe changes on the model and update its UI elements appropriately.

2012-04-04 21:16
by Rob Napier
I am looking for a way to programatically notified that a view is loaded from outside of that viewController. I understand that to communicate between classes I could use NSNotificationCenter, or use a delegate method or tag the view, etc. But I don't want to change anything in the source code of the view controller. I want to have lets say sth like the library or framework to manage this from outside. All my searches so far not that helpful, looks like there are some Aspect Oriented Programming in Objective-C which I may need to look - ramo 2012-04-04 21:34
Can you explain why you want to access a view controller's private information (its views, which it is solely responsible for managing)? Breaking MVC will tend to bite you in Cocoa. The tool you would use here is KVO (Key Value Observing), but it strongly suggests a design issue - Rob Napier 2012-04-04 21:46
Ads