I don’t understand why adding the init method to my view controller sets some ivars to (null) when some data are passed to this controller. Here’s the code I use to set some ivar values and call the associated view controller:
MyViewController *vc = [[MyViewController alloc] init];
vc.search_type = [NSMutableString stringWithString:@"buy"];
[self.navigationController pushViewController:vc animated:NO];
In MyViewController, the search_type ivar contains the @"buy" string stored in the previous view controller as expected. However, when I add the init method below, search_type always contains a (null) string when I try to use it in MyViewController:
(id)init
{
self = [super init];
if(self)
{
// some other code here
}
return self;
}
search_type is declared in interface as:
@property (nonatomic,retain) NSMutableString *search_type;
and synthesize in implementation file:
@synthesize search_type;
It’s not entirely clear what you are trying to do, but if you are saying that
search_typeis nil in yourinitmethod where you have placed// some other code here, that’s because your view controller is initialised before you set that property.If you need to supply that object to your view controller before some more code runs, you have several options:
initWithSearchType:.viewDidLoad,viewWillAppearor similar method.Which of these is appropriate depends on exactly what it is you are doing.