Parameter passing with initWithNibName:

这一生的挚爱 提交于 2019-12-08 21:25:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!