MVC and Singleton in HelloPoly

3 views
Skip to first unread message

Michael Z

unread,
Dec 20, 2009, 4:55:04 AM12/20/09
to iPhone Application Development Auditors
Hi:
I have a PolygonShape instance variable(ivar) in view, and another
PolygonShape ivar in controller. These two ivar should represent the
same PolygonShape, to be more precise they points to the same memory
address, update to ivar in controller would be reflected in view, vice
and versa. This can be achieved by making the ivars in view and
controller prefixed with IBOutlet and then link the two ivars to
PolygonShape in IB. How can this be achieved in code rather than in
IB?

Thanks in advance..

Sukima

unread,
Dec 20, 2009, 11:09:44 AM12/20/09
to iPhone Application Development Auditors
I think you confused what a pointer is for. IB makes pointer
references. It's kind of a go around the long way thing.

What your interested in is getters and setters. Your controller will
send a setter message to your view with the object. The view saves the
reference as a pointer to the original object. It's still the same
object. There are several ways to do this. It's the same a
NSMutableArray's addObject: method. It takes the pointer reference to
the object and saves it. There is still one copy of the object though
only many pointers to it.

I made a simple example of two ways you can pass the object to your
view and still do what you want without IB. Example is here:

http://gist.github.com/260541

Qiuyuan Zhang

unread,
Dec 20, 2009, 2:53:04 PM12/20/09
to iphone-appd...@googlegroups.com
Hi Sukima, Thanks very much for your explanation and sample code.
That was really helpful.

I have related questions. Thanks to anyone in advance can help.
In my previous post, I mentioned two PolygonShape ivars, one in Controller and one in View, and they point to the same memory address(represents the same object). You suggested it is done by having Controller to send a setter message to view with the object. eg in Controller class:
myView.viewPolygon = myPolyObject;

Continuing from my previous post, what if we add another view? Let say, there are Controller, Object(PolygonShape),View1 and View2. Controller,View1 and View2 should point to the same object. How can this be done? Deduct from your suggestions, then it can be done in Controller class:
myView1.viewPolygon = myPolyObject;
myView2.viewPolygon = myPolyObject;


Then another question follows, what happens in controller if update to PolygonShape in myView1? eg, numberOfSides is updated to 5? Is the following code correct way to make sure two views reflects the same PolygonShape in controller?

myView1.viewPolygon.numberOfSides = numberOfSides;//5
myView2.viewPolygon.numberOfSides = numberOfSides; //Is this line useless? since myView1.viewPolygon and myView2.viewPolygon points to the same PolygonShape. only one update is needed?

Should there be a better way, somehow only one update is done?


Thanks again in advance to anyone who can help.



--

You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.
To post to this group, send email to iphone-appd...@googlegroups.com.
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en.





--
秋渊(Michael) Zhang

Sukima

unread,
Dec 21, 2009, 12:23:05 AM12/21/09
to iPhone Application Development Auditors
You are correct in your assessments. You would only need to set the
number of sides to one variable.

The idea behind this is that each object (controller, view, etc) only
knows about it's own reference. You change the ivar in your controller
(basically your model of the MVC) it will propagate to the view.

Things you have to remember is how the setters handle the references.
Is it a retain/release model? Is it a copy/release? Is it an assign
only? These affect if your referencing the same object or a copy. And
wether your guaranteed a valid object no matter where you look or if
it can be released under your nose.

> > iphone-appdev-aud...@googlegroups.com<iphone-appdev-auditors% 2Bunsu...@googlegroups.com>

Michael Z

unread,
Dec 21, 2009, 2:45:56 AM12/21/09
to iPhone Application Development Auditors
Hi Sukima:
Thanks so much for your reply, it was very helpful.
I have two more related question. First question:Continue from what
you said, copy, assign and retain affects the implementation.
I am unsure if my understanding of assign, retain or copy is correct,
i would really appreciate if you can confirm my understanding on them
Say there are
@interface PolygonView{
PloygonShape *plogygon1;
PloygonShape *plogygon2;
PloygonShape *plogygon3;
}
@property (assign) PloygonShape *polygon1;
@property (retain) PloygonShape *polygon2;
@property (copy) PloygonShape *polygon3;

@end;

The sythesized getters are all the same. each simply returns.
-(PolygonShape *)polygon1 { return polygon1;}
-(PolygonShape *)polygon2 { return polygon2;}
-(PolygonShape *)polygon3 { return polygon3;}

The difference lies where how setters are synthesized, the equivalents
are:
-(void)setPolygon1:(PolyggonShape *value){ polygon1 = value;} //simply
pointer assignment, no retain nor release. polygon1's retain count is
the same,polygon1
// and value points to the same object, update to object pointed
bypolygon1 would update value as well.

-(void)setPolygon2:(PolyggonShape *value)
{
if(polygon2 != value)
{
[polygon2 release];
polygon2 = [value retain]; //polygon2' retain counted
INCREASED by 1
}

}
//polygon2 and value points to the same object, update to object
pointed bypolygon1 would update value as well.

-(void)setPolygon1:(PolyggonShape *value){
if(polygon3 != value){
[polygon3 release];
polygon3 = [value copy]; //polygon3' retain count IS 1,
polygon3 is a new copy,update to polygon3 WOULD NOT AFFECT value.
}

}

Rule of thumb from lecture slides, not sure if it is right.
use assign for primitive types, integer.
use copy for NSString
user retain for others.

Can you please confirm this?
Thanks a great deal

Byron Cooke

unread,
Dec 21, 2009, 5:24:48 AM12/21/09
to iphone-appd...@googlegroups.com
The documentation "The Objective C Programming Language" on the dev
website has a very good explanation of all three setter semantics..
Look under declared properties
Reply all
Reply to author
Forward
0 new messages