1

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.

Michael Mangold
  • 365
  • 1
  • 2
  • 11
  • 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

1 Answers1

1

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.

hotpaw2
  • 7,938
  • 4
  • 21
  • 47