Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter passing with initWithNibName:

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

like image 796
Chathura Palihakkara Avatar asked Jun 06 '11 07:06

Chathura Palihakkara


1 Answers

You should create another initializer in your class, something like

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andParam:(id)aParam {
...
self.param = aParam;
}
like image 92
Bolek Tekielski Avatar answered Sep 30 '22 17:09

Bolek Tekielski