Considering I have a UIViewController
called ErrorViewController
that I am instantiating using initWithNibName
.
考虑到我有一个叫做ErrorViewController的UIViewController我用initWithNibName实例化它。
There is a enum on this ErrorViewController
describing its "type".
这个ErrorViewController上有一个enum,描述它的“类型”。
This ErrorViewController
has one delegate function that returns to its delegate which will respond according to the type set on the ErrorViewController
.
这个ErrorViewController有一个委托函数返回给它的委托它会根据ErrorViewController的类型集进行响应。
Is it better to pass all the parameters within a new initWithNibName
function, and set private properties on the ErrorViewController
. Like this:
在新的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;
}
Or is it better to instantiate the object and afterward set its public properties like this:
还是实例化对象,然后设置它的公共属性,比如:
ErrorViewController *errorVc = [[ErrorViewController alloc]
initWithNibName:@"ErrorViewController" bundle:nil];
errorVc.delegate = self;
errorVc.type = kErrorTypeA.
And regarding the delegate method, is it best practice to check the type by passing a parameter, or by checking the property of the passed back Controller as follows:
对于委托方法,是通过传递一个参数来检查类型,还是通过如下方式检查传递回来的控制器的属性来检查类型:
- (void)ErrorPage:(ErrorViewController *)ErrorPage
// check ErrorPage.errorType
}
or this: ?
或:?
- (void)ErrorPage:(ErrorViewController *)ErrorPage
andErrorType:(ErrorType)errorType
// check errorType
}
1 个解决方案
#1
2
I think it's a question of preference. If the object can't function correctly without error-type and/or delegate, it's probably best to provide your custom initialiser.
我认为这是一个偏好问题。如果对象在没有错误类型和/或委托的情况下不能正常工作,那么最好提供自定义初始化器。
As to your second question, I would provide the error type as in your second example. Note that the method name should start with a lowercase character though (-errorPage:
instead of -ErrorPage:
).
关于第二个问题,我将提供第二个示例中的错误类型。注意,方法名应该以小写字符开头(-errorPage:而不是-errorPage:)。
Additionally, if you use it a lot, I would provide a convenience class method to create the object:
另外,如果您经常使用它,我将提供一个方便的类方法来创建对象:
+(ErrorViewController*) standardErrorViewControllerWithErrorType: (ErrorType) errorType andDelegate: (id<ErrorDelegate>) delegate {
ErrorViewController *evc = [[ErrorViewController alloc] initWithNibName: @"ErrorViewController" bundle: nil andErrorType: errorType andDelegate: delegate];
return evc;
}
Edit
编辑
Also, in your init method, it is encouraged to use -(instancetype) init...
instead of -(id) init....
另外,在init方法中,鼓励使用-(instancetype) init…(而不是)- id init ....
#1
2
I think it's a question of preference. If the object can't function correctly without error-type and/or delegate, it's probably best to provide your custom initialiser.
我认为这是一个偏好问题。如果对象在没有错误类型和/或委托的情况下不能正常工作,那么最好提供自定义初始化器。
As to your second question, I would provide the error type as in your second example. Note that the method name should start with a lowercase character though (-errorPage:
instead of -ErrorPage:
).
关于第二个问题,我将提供第二个示例中的错误类型。注意,方法名应该以小写字符开头(-errorPage:而不是-errorPage:)。
Additionally, if you use it a lot, I would provide a convenience class method to create the object:
另外,如果您经常使用它,我将提供一个方便的类方法来创建对象:
+(ErrorViewController*) standardErrorViewControllerWithErrorType: (ErrorType) errorType andDelegate: (id<ErrorDelegate>) delegate {
ErrorViewController *evc = [[ErrorViewController alloc] initWithNibName: @"ErrorViewController" bundle: nil andErrorType: errorType andDelegate: delegate];
return evc;
}
Edit
编辑
Also, in your init method, it is encouraged to use -(instancetype) init...
instead of -(id) init....
另外,在init方法中,鼓励使用-(instancetype) init…(而不是)- id init ....