开发者

initializing UIView subclass with parameters in iphone sdk?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜