Reputation: 2809
I created a simple UIViewController with a custom init method like this:
-(id)initWithGroupNumber:(NSString *)groupNumber {
if ((self = [super init]) == nil) {
return nil;
}
self.levelGroup = groupNumber;
return self; }
levelGroup is a property written in the .h file
@property (nonatomic, retain) NSString *levelGroup;
When I call the method above this way:
LevelsViewController *lvc = [[LevelsViewController alloc]initWithGroupNumber:@"5"];
the controller is allocated but all the property inside are set to nil. I can't understand why.
Upvotes: 0
Views: 432
Reputation: 33428
First of all when you deal with classes that have subclass of type mutable (e.g. NSMutableString
), use copy
.
So, your property should become:
@property (nonatomic, copy) NSString *levelGroup;
Then, inside the UIViewController
synthesize the property
@synthesize levelGroup;
and in init
do the following:
-(id)initWithGroupNumber:(NSString *)groupNumber {
if (self = [super init]) {
levelGroup = [groupNumber copy];
}
return self;
}
As written in the memory management guide you should not use self.
inside init
and in dealloc
.
Use your property self.levelGroup
to get or set the value.
Remember to release in dealloc:
[levelGroup release];
Hope it helps.
Upvotes: 2
Reputation: 9933
Replace your
self.levelGroup = [[NSString alloc]init];
with
self.levelGroup = groupNumber; // actually uses your init value.
Upvotes: 2