0

I have a container view that holds a view controller. I need to set a non-UI property in this view controller before awakeFromNib is called. However, the prepareForSegue method for the embed segue isn't called until after awakeFromNib happens.

Is there any way to pass this information to the contained view controller before awakeFromNib?

Aaron Brown
  • 407
  • 4
  • 17
  • You're asking how to initialise something before it's initialiser is called? Is there anything wrong with just setting a property in `prepareForSegue:`? – Abizern Jun 25 '13 at 00:28
  • A certain property has to be set before awakeFromNib, and prepareForSegue doesn't accomplish that. This is required by the superclass of this view controller. I suppose I could modify that code but it is third party. I'm just wondering if there is any way to, for example, create a custom initializer that can be called directly while still using a container view in storyboard. – Aaron Brown Jun 25 '13 at 00:36
  • 1
    Which third party code? It seems odd that it needs to do that. – Abizern Jun 25 '13 at 01:03
  • No, not if you're instantiating from the storyboard. – Wain Jun 25 '13 at 07:01

1 Answers1

1

I have a similar issue in one of my apps.

Basically, I have a ViewController that has a property for the data model, but I am never sure when in my lifecycle the data model is actually set. My solution was to use Key-Value Observing to receive a callback when it's set.

Somewhere before the value can be set:

[self addObserver:self forKeyPath:@"propertyName" options: 0 context: nil];

Callback:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

   if ([keyPath isEqualToString:@"propertyName"]) {   
      //do something
   }

}

remember to unregister (I do it in my dealloc)

[self removeObserver:self forKeyPath:@"propertyName"];
StaRbUck42
  • 117
  • 1
  • 8