在UIViewController上添加UIView [英] Add UIView on UIViewController

查看:352
本文介绍了在UIViewController上添加UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在uiviewcontroller上添加一个自定义 uiview,我用xib创建了这个自定义视图,它与视图控制器是分开的,

I want to add a "custom" uiview onto a uiviewcontroller, this custom view i created with xib and its a seperate from the view controller,

有人知道吗如何将带有xib的uiview添加到uiviewcontroller中?

does anyone know how to add a uiview with a xib into a uiviewcontroller?

在此先感谢

推荐答案

您的意思是另外一个观点,不是主控制器视图?在这种情况下,您可以为视图声明属性并手动加载NIB:

You mean an additional view, not the main controller view? In that case you can declare a property for the view and load the NIB by hand:

@interface Controller {}
@property(retain) IBOutlet UIView *extraView;
@end

…

- (void) viewDidLoad // or anywhere else
{
    [[NSBundle mainBundle] loadNibNamed:@"extras" owner:self options:nil];
    NSAssert(extraView != nil, @"The extra view failed to load.");
    [[self view] addSubview:extraView];
}

这假设您设置了 Controller 作为Interface Builder中的文件所有者,您将视图链接到 extraView 出口。还要注意,可能会有更优雅的解决方案,例如将额外的视图插入控制器的主NIB中。视情况而定。

This assumes that you set the Controller as the file owner in the Interface Builder and you link the view to the extraView outlet. Also note that there might be more elegant solutions, like inserting the extra view into the main NIB for your controller; depends on the situation.

这篇关于在UIViewController上添加UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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