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

查看:164
本文介绍了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:

Obj-C:

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

迅速:

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


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


That's fine, but in order to initialize the UIView part of the object, your method must call the designated initializer:

Obj-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天全站免登陆