自定义uiview中控件的初始化 [英] Initialization of controls in custom uiview

查看:290
本文介绍了自定义uiview中控件的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IB中创建了一个自定义uiview并为其设置了类。

I've created a custom uiview in IB and set the class for it.

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface myView : UIControl {
    IBOutlet UITextView *textView;
}
@end

#import "myView.h"

@implementation myView

- (void)commonInit
{
    [textView setText:@"lajsdfklasdfjl"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self commonInit];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self commonInit];
    }
    return self;
}

@end

我把带有文字的文字视图在IB中的这个视图并将其链接到IBOutlet IUTextView * textView。
当我在我的UIViewController上拖动这个自定义视图时(从IB的库中的类选项卡),视图为空。
- (id)initWithCoder:(NSCoder *)aDecoder正在调用但textView为null。
有什么问题?谢谢

I put a textview with text on this view in IB and linked it to IBOutlet IUTextView *textView. When I drag this custom view on my UIViewController (from classes tab in IB's library) the view is empty. - (id)initWithCoder:(NSCoder *)aDecoder is calling but the textView is null. What is wrong? Thanks

推荐答案

据我记忆,在init中没有正确设置层次结构,因为属性只能在之后设置init已经完成。

As far as I remember, the hierarchy is not properly set up in init, as the properties can only be set after init has finished.

你想使用

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self commonInit];
}

而是删除 initWithCoder:完全没事。

作为旁注,你应该让你的班级名字以大写字母开头。

As a side note, you should let your class names begin with upper case letters.

这篇关于自定义uiview中控件的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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