问题
In iphone Application I need to pass some values to a new viewcontroller object while it create from a method in another viewcontroller class so I can initialize that values in (id)initWithNibName:method of new viewcontroller then I can load those values in viewdidLoad method.
what I want to know is how do I pass the values(parameters) to the constructor(initWithNibName) of a new viewcontrollor Object like constructor overloading in java give me some code example just showing how initWithNibName called with extra parameters and how to retrieve them in the newly created object Thanks...
Answer
this is the way I solve the problem "Observation is a object with attributes" in ViewControllor.h I put
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(Observation *)observation;
in ViewControllor.m file I put
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(Observation *)observation{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization on passed parameter observation object
draftObservation = observation;
}
return self;
}
then I call it this way in another class
ObsevationListView *obsevationListView = [[ObservationViewControllor alloc]
initWithNibName:@"ObservationViewControllor"
bundle:nil set:observer];
[self.navigationController pushViewController:obsevationListView animated:YES];
it works fine. I'm glad if anyone get help from this
回答1:
You should create another initializer in your class, something like
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andParam:(id)aParam {
...
self.param = aParam;
}
回答2:
Another solution is to have a property for that parameter and set it either before or after you call using the following code:
initWithNibName:bundle:
来源:https://stackoverflow.com/questions/6248944/parameter-passing-with-initwithnibname