In order for your @property
to retain the mainView
, you should use it as self.mainView
and not just mainView
. If you use the latter alone, it will not retain it. Basically if you call self.mainView = ...
it is calling a setter method for mainView
which does a [mainView retain];
internally. When you are directly assigning it, it wont execute this setter and retain will not be executed.
You should try it like this,
self.mainView = [[[UIView alloc] init] autorelease];
[self.view addSubView:self.mainView];
or as shown in your question.
UIView *newView = [UIView alloc] init];
self.mainView = newView;
[newView release];
[self.view addSubView:self.mainView];
You can also try using ARC for your project. Your code will look like this in ARC,
self.mainView = [[UIView alloc] init];
[self.view addSubView:self.mainView];
Check the documentation for more details.