tags:

views:

741

answers:

1

Iam a newbiew to iPhone development. Version of my SDK is 2.2

In my code, UIViewController is used to change view dynamically once the app is launched, a method in the UIViewController is called to know which view should be initialized along with parameters, which goes to a 'switch-case' and assign a view to current view according to the parameter, like this:

    case 1:
  currentView = [[View01 alloc] init];
  break;
 case 2:
  currentView = [[View02 alloc] init];
  break;

and outside the switch-case:

[self.view addSubview:currentView.view];

I wonder f can pass a parameter along with initialization, like iniWithNibName or so? I need this because have to manipulate in the leaded view, according to the view from which its called.

Thanks.

A: 

One way to approach this is to modify your View01 and View02 classes to include an initWithParam: initialiser.

i.e. add

- (id) initWithParam:(NSString *)myParam;

to the @interface section and add

- (id) initWithParam:(NSString *)myParam {
  if (self = [self init]) {
    // handle or store 'myParam' somewhere for use later
  }

  return self;
}

to the @implementation section. Notice how the initWithParam: message internally calls the existing init. Obviously you could change the type, or number of parameters passed in as required.

Another approach would be to provide a property on your view class, so you could do something like the following:

currentView = [[View01 alloc] init];
currentView.myParam = @"SomeValue";

Which approach works the best will depend somewhat on your particular application needs.

Christopher Fairbairn
I was doing it like your second way, first one is what exactly I was looking for. Thanks!
pMan
you could have initWithParam and also the property in case you need to change the initial value set.
Joo Park