1

我有兴趣编写我的自定义视图,所以我创建了以下 xib 文件:

在此处输入图像描述

这是定义文件:

- (void)_baseInit {
    NSLog(@"Unseen View loaded");
    [self addSubview:[self activityIndicator]];
    [self activityIndicator].alpha = 1.0;
    [self activityIndicator].frame = CGRectMake(round(([self imageView].frame.size.width - 25) / 2),
                                                round(([self imageView].frame.size.height - 25) / 2), 25, 25);
    [self activityIndicator].hidesWhenStopped = YES;
    [self showIndicator];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [self imageView].userInteractionEnabled = YES;
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [[self imageView] addGestureRecognizer:panRecognizer];

}

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

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

我试图将它连接到我的故事板中:

X

我让我的 MainViewController 在 vi​​ewDidLoad 期间调用它:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.unseenView = [[[NSBundle mainBundle] loadNibNamed:@"UnseenView" owner:self options:nil] objectAtIndex:0];
    self.unseenView.delegate = self;

不幸的是,我的模拟器中什么也没有显示,甚至文本标签也没有在此处输入图像描述

但是我看到以下日志消息:

2013-02-20 17:37:58.929 Giordano.iPhone[66857:c07] Unseen View loaded
2013-02-20 17:37:58.934 Giordano.iPhone[66857:c07] Unseen View loaded

我究竟做错了什么?

4

1 回答 1

1

看起来您正在正确准备视图,但您没有将其添加到视图层次结构中。在viewDidLoad您需要添加此行的代码中:

[self.view addSubview:unseenView];
于 2013-02-21T01:52:17.637 回答