0

我知道如何在 xib 中布局和创建 UIView,
但现在我有一个自定义视图(UIView 的子视图),
它有不同的 init 方法,例如:

-(id)initWithArg1:(int) arg2:(NSString *) arg3:(NSArray *) ...

或者

+(id)viewWithArg1:(int) arg2:(NSString *) arg3:(NSArray *) ...

当我在 xib 属性栏中将视图的类设置为我的时,
我的自定义 init 方法没有出现,无处可设置。

又怎样?非常感谢。

4

3 回答 3

1

您需要覆盖initWithCoder:并使用一些合理的默认值来完成指定的初始化程序正在执行的操作。

例如

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        _value1 = 0;
        _value2 = @"Hi!";
        /* and so on */
    }

    return self;
}
于 2012-09-04T07:51:04.393 回答
0

我认为您不需要init在 xib 中设置方法,在其他地方您只需init像这样调用 by 代码:

[[yourView alloc] initWithArg1:arg1 Arg2:arg2];
于 2012-09-04T07:52:37.023 回答
0

卡尔是正确的。

本质上,如果一个对象是从 XIB 实例化的(按照您在问题中提到的步骤),那么被调用的初始化程序是

- (id)initWithCoder:(NSCoder *)aDecoder;

You can confirm this for yourself by over-riding every possible initialiser and putting breakpoints in all of them to see which is called.

So, If you are initialising from the XIB it kind of hints that you will have one instance of that object (not necessarily so); however if you need lots of instances of a custom object then you might consider going back to a custom initialiser and forgo the XIB work by creating everything programatically.

In essence, Think how you are going to be using the object. This will tell you if you need to be thinking of custom initialisers that create their own view programatically.

于 2012-09-04T08:25:37.893 回答