考虑到我有一个UIViewController
调用ErrorViewController
,我正在使用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
}