iOS:UIView 子类 init 或 initWithFrame:? [英] iOS: UIView subclass init or initWithFrame:?

查看:28
本文介绍了iOS:UIView 子类 init 或 initWithFrame:?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有固定框架的 UIView 子类.那么,我可以覆盖 init 而不是 initWithFrame: 吗?例如:

I made a subclass of UIView that has a fixed frame. So, can I just override init instead of initWithFrame:? E.g.:

- (id)init {
    if ((self = [super initWithFrame:[[UIScreen mainScreen] bounds]])) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-initWithFrame: 的 Xcode 文档说:如果您以编程方式创建视图对象,则此方法是 UIView 类的指定初始值设定项.子类可以覆盖它方法来执行任何自定义初始化,但必须在其实现开始时调用 super."

The Xcode documentation for -initWithFrame: says: "If you create a view object programmatically, this method is the designated initializer for the UIView class. Subclasses can override this method to perform any custom initialization but must call super at the beginning of their implementation."

指定初始值设定项"是什么意思?

What does "designated initializer" mean?

推荐答案

指定的初始化程序是所有其他初始化程序必须调用的.UIView 和子类有点不寻常,因为它们实际上有两个这样的初始化器:-initWithFrame:-initWithCoder:,具体取决于如何视图已创建.如果您在代码中实例化视图,您应该覆盖 -initWithFrame:,如果您从笔尖加载它,则应该覆盖 -initWithCoder:.或者,您可以将您的代码放在第三个方法中并覆盖这两个初始值设定项,以便它们调用您的第三个方法.事实上,这通常是推荐的策略.

The designated initializer is the one that all the other initializers must call. UIView and subclasses are a little unusual in that they've actually got two such initializers: -initWithFrame: and -initWithCoder:, depending on how the view is created. You should override -initWithFrame: if you're instantiating the view in code, and -initWithCoder: if you're loading it from a nib. Or, you could put your code in third method and override both those initializers such that they call your third method. In fact, that's often the recommended strategy.

因此,例如,您可以创建一个 UIView 子类 ClueCharacter,它有自己的初始化方法:-initWithPerson:place:thing:.然后像这样创建视图:

So, for example, you might create a UIView subclass, ClueCharacter, that has its own initialization method: -initWithPerson:place:thing:. You then create your view like this:

对象-C:

ClueCharacter *mustard = [[ClueCharacter alloc] initWithPerson:@"Col. Mustard"
                                                         place:kInTheStudy
                                                         thing:kTheRope];

斯威夫特:

var mustard = ClueCharacter("Col. Mustard", place: kInTheStudy, thing: kTheRope)

<小时>

那很好,但是为了初始化对象的 UIView 部分,你的方法必须调用指定的初始化器:

对象-C:

-(id)initWithPerson:(NSString*)name place:(CluePlace)place thing:(ClueWeapon)thing
{
    if ((self = [super initWithFrame:CGRectMake(0, 0, 150, 200)])) {
        // your init stuff here
    }
}

斯威夫特:

func init(name: String, place : CluePlace, thing : ClueWeapon)
{
    if (self = super.init(CGRectMake(0, 0, 150, 200))) {
       // your init stuff here
    }
}

<小时>

如果你想调用你子类的初始化器-init,只要你在实现中调用-initWithFrame:就可以了.


If you want to call your subclass's initializer -init, that's okay as long as you call -initWithFrame: in the implementation.

这篇关于iOS:UIView 子类 init 或 initWithFrame:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆