【发布时间】:2013-06-25 12:03:13
【问题描述】:
考虑到我有一个名为ErrorViewController 的UIViewController,我正在使用initWithNibName 进行实例化。
ErrorViewController 上有一个枚举,描述了它的“类型”。
此ErrorViewController 有一个委托函数,该函数返回其委托,该委托将根据ErrorViewController 上设置的类型进行响应。
在新的initWithNibName 函数中传递所有参数是否更好,并在ErrorViewController 上设置私有属性。像这样:
ErrorViewController *errorVc = [[ErrorViewController alloc]
initWithNibName:@"ErrorViewController" bundle:nil
andErrorType:kErrorTypeA andDelegate:self];
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
andErrorType:(ErrorType)errorType andDelegate:(id<ErrorDelegate>)delegate{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.delegate = delegate;
self.errorType = errorType;
}
return self;
}
或者最好先实例化对象,然后像这样设置它的公共属性:
ErrorViewController *errorVc = [[ErrorViewController alloc]
initWithNibName:@"ErrorViewController" bundle:nil];
errorVc.delegate = self;
errorVc.type = kErrorTypeA.
关于委托方法,最佳实践是通过传递参数来检查类型,还是通过检查传回的Controller的属性如下:
- (void)ErrorPage:(ErrorViewController *)ErrorPage
// check ErrorPage.errorType
}
或者这个:?
- (void)ErrorPage:(ErrorViewController *)ErrorPage
andErrorType:(ErrorType)errorType
// check errorType
}
【问题讨论】:
标签: iphone objective-c uiviewcontroller enums