I therefore declared it in the @interface section
right, that just reserves space and a label to access an ivar with the type+name
and then @property (nonatomic, retain) UIView *calendarView.
right, that declares the accessors (setter+getter), and operation if synthesized
In the @implementation I have @synthesise calendarView.
that defines (implements) the accessors declared by the property declaration.
Now I would like to define the frame of this view and coded the following:
...
But I am now wondering if I am doing something seriously wrong here, as calendarView has already been retained and synthesised. Surely UIView alloc is superfluous or even bound to make the app crash, as I am running into memory problems, right?
for one, your memory management is off:
CGRect calendarFrame = CGRectMake(170, 8, 200, 50);
UIView * view = [[UIView alloc] initWithFrame:calendarFrame];
self.calendarView = view; // use the setter, unless in init... or dealloc
[view release], view = 0;
two:
you will not usually have much use in creating a UIView. typically, you will create a subclass of it.
three (to get to your question):
there's nothing wrong with that. the variable will be nil
until it's been set. the field is not default initialized for the declared type -- well, it is, but the type is in fact a pointer, so the result is that it is initialized to nil. you can either create a view or pass it in from someplace else. the view will be nil/NULL/0 until that point.
So I though I should code this instead of the two previous lines, but it only had the effect that the calendarView is not shown at all:
[calendarView setFrame:CGRectMake(170, 8, 200, 50)];
So my question is if I really need to alloc the view before I can use it? Or is there yet another solution?
stepping back to point #2 in more detail: you'll want to create a subclass in most cases. UIView/NSView does no drawing by default, but it can be used as a view container. therefore, you may want to start with some existing subclasses to get familiar with the system-supplied views.
once you have a handle on that, try implementing your own subclasses and overriding drawRect:
.
many beginners like using Interface Builder (now integrated into Xc4) -- a WYSIWYG view editor.