Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm new to OOP, having been a procedural programmer for many years. I'm learning iOS development in Objective-C and could use some clarity on when and how objects and properties are used.

Say I have a view controller, MyViewController. Within that is a UIView, MyView, with a property, viewProperty.

How do I reference viewProperty from within MyViewController? My hunch is that I need to instantiate an instance of MyView, say:

MyView *instanceView;

and then reference instanceView.viewProperty. But without having instantiated a MyView at all, a MyView view shows up in MyViewController when I run the iPhone Simulator. Is that because my XIB file implicitly instantiates an instance of MyView? If so, how do I reference that instance in code? If I have to instantiate a MyView instance within MyViewController, which method(s) should contain the declaration, and why?

I hope I explained my foggy perceptions adequately for someone to clear things up for me. Apologies if I'm not making sense, but I feel like if someone just connects the dots for me I'm almost there.

share|improve this question
    
This belongs on SO. Specific question, not subjective. –  P.Brian.Mackey Jul 18 '11 at 18:37
    
@P.Brian: Programmers.SE is for conceptual, whiteboard-y questions; Stack Overflow is for implementation, issue-while-I'm-coding questions. –  Robert Harvey Jul 18 '11 at 18:46
    
"How do I reference viewProperty from within MyViewController? " is asking for an implementation. He needs an implementation to solve the reference problem. The initial question and title are misleading and really a different topic. So, I suggest the OP either create two distinct questions (one for OO best practices and another for the reference problem) or consolidate this one into something on topic for one of the two sites. –  P.Brian.Mackey Jul 18 '11 at 18:49
add comment

1 Answer

up vote 1 down vote accepted

How do I reference viewProperty from within MyViewController?

You shouldn't. In the OOP pattern, your controller object should send a message to the view object if it needs to update some property information in the view or get some state information back.

A XIB instantiates objects when it is loaded, you can hook them up to code in Interface Builder if you need to reference those objects from code. Or you can create objects programmatically when your controller initialization methods are called and not use a .xib file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.