PDA

View Full Version : For the more curious....Hillegas...page 121




mdeh
Mar 29, 2009, 01:35 PM
Hi all,
At the end of each chapter, are little, I guess, extra bits of knowledge, for the "more curious" as to the workings of Cocoa. Well, unfortunately, I am one of those curious..and am not sure if the author means that the reader should be able to use the book's prior information presented to solve these issues. Anyway, perhaps the list would not mind if I delve into this a little more deeply. (Chapter 7...Key-Value coding, Key-Value Observing)



The issue is how to ( if I read this correctly) establish KVO programmatically.

Hillegass says: ( Here, the set up is... briefly. A window with a slider, and a label. The slider is bound to the AppController which acts as model/controller, updating a property of the AppController. In the initial setup, the label is bound to the property as an observer, and is updated for any changes in the value of the property...which works as expected.)

How did the text field become an obsever of the "fido" key in the AppController Object? When it wakes up from the nib, the text field adds itself as an observer.

Using this code.

[AppController addObserver:self forKeyPath:fido options:KeyValueObservingOld context:somePointer]

From the appleDocumentation:

An awakeFromNib message is sent to each object loaded from the archive, but only if it can respond to the message, and only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set.


I am trying to programmatically ( being more curious :) ) to do this, but there are many areas of confusion for me.


1)
Does Apple's documentation mean that each object in the nib, once fully initialized receive an "awakeFromNib"...so, Window will be loaded, and then once ready, Window will receive and awakeFromNib, or as **each** object within window is ready, ( slider, button, textfield) **each** one will receive an awakeFromNib?

2) As the code used by Hillegas is an instance method, **where** would one place this code. It seems to imply that it would have to be placed in a class that represented the label? Or is this really just a curiosity, and not practical?

Thank you in advance?



eddietr
Mar 29, 2009, 03:53 PM
1)
Does Apple's documentation mean that each object in the nib, once fully initialized receive an "awakeFromNib"...so, Window will be loaded, and then once ready, Window will receive and awakeFromNib, or as **each** object within window is ready, ( slider, button, textfield) **each** one will receive an awakeFromNib?


Each object will receive awakeFromNib after all objects have been initialized and all outlets have been set.


2) As the code used by Hillegas is an instance method, **where** would one place this code. It seems to imply that it would have to be placed in a class that represented the label? Or is this really just a curiosity, and not practical?

awakeFromNib is a good place, because you know you'll have your outlets set to initialized objects at that point.

mdeh
Mar 29, 2009, 04:37 PM
Each object will receive awakeFromNib after all objects have been initialized and all outlets have been set.

Eddie..just to make sure I am expressing myself correctly. By object you mean each file in the nib...(Window, [custom]Oject, Controller etc). What about the controls **within** a window(object), like the textfields, sliders, buttons etc. (I think you are saying that the awakeFromNib will be sent to the actual object in the nib, not each individual control within the object?)



awakeFromNib is a good place, because you know you'll have your outlets set to initialized objects at that point.


Depending upon how you answer the above ...and this may well be getting ahead of the book... would I have to have an object representing **that** particular (control) eg label called for example "labelController" and in *that* object's awakeFromNib implement the method addObserver: .....
Hope this all makes sense.

eddietr
Mar 29, 2009, 04:58 PM
Eddie..just to make sure I am expressing myself correctly. By object you mean each file in the nib...(Window, [custom]Oject, Controller etc). What about the controls **within** a window(object), like the textfields, sliders, buttons etc. (I think you are saying that the awakeFromNib will be sent to the actual object in the nib, not each individual control within the object?)


Well, each of those is an object. So if you have three UITextFields (or whatever), that's 3 objects (in addition to the other objects).

Each of those will get awakeFromNib.


Depending upon how you answer the above ...and this may well be getting ahead of the book... would I have to have an object representing **that** particular (control) eg label called for example "labelController" and in *that* object's awakeFromNib implement the method addObserver: .....
Hope this all makes sense.

Well, I would use the word "controlling" rather than "representing", but yes that's one way to do it.

mdeh
Mar 29, 2009, 05:17 PM
Well, I would use the word "controlling" rather than "representing", but yes that's one way to do it.


Good...thanks. You know...half, if not more with these languages is syntax...and often my confusion is due to not using the correct syntax. What is a little confusing with these side-journeys, is that I am never sure if Hillegass means one can use the information provided *so far* to implement something ( the above being a good example) or whether one has to go off on a tangent to figure this out.

eddietr
Mar 29, 2009, 05:21 PM
Good...thanks. You know...half, if not more with these languages is syntax...and often my confusion is due to not using the correct syntax. What is a little confusing with these side-journeys, is that I am never sure if Hillegass means one can use the information provided *so far* to implement something ( the above being a good example) or whether one has to go off on a tangent to figure this out.

Yep, like any other subject, the people who get lazy with the nomenclature (or don't think it's important) early on will usually struggle for a very long time. That's true for any subject.

So I went back and read the page in Hillegass that you're talking about. And I don't think he was trying to get you to solve anything there. He was just giving some additional background on the subject.

mdeh
Mar 29, 2009, 05:23 PM
So I went back and read the page in Hillegass that you're talking about. And I don't think he was trying to get you to solve anything there. He was just giving some additional background on the subject.

Sadly...I am **too** curious? :D I was trying to implement this on my own...but soon ran into those roadblocks. I will mark it as something to come back to.