如何一起使用xib和一个UIView子类? [英] How to use a xib and a UIView subclass together?

查看:75
本文介绍了如何一起使用xib和一个UIView子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建几个自定义视图,一系列可重用的UI组件,并且不希望在UIView子类中的代码中布置UI。我想使用一个xib。我自己使用了一个xib。我用iteself使用了一个UIView子类。但我没有一起使用它们。我如何附加他们彼此?我想在自定义视图中使用IBOutlets访问UILabels。

I'd like to create a couple of custom views, sort of reusable UI components, and would prefer to not layout the UI in code in the UIView subclass. I'd like to use a xib for that. I've used a xib by itself. And I've used a UIView subclass by iteself. But I haven't used them together. How do I "attach" them to one another? I'd like to use IBOutlets to access the UILabels in my custom view.

这种事情是否有效?

NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; 
MyCustomView *view = [xib objectAtIndex:0];
view.myLabel.text = @"fred";


推荐答案

是的,将非父级的xibs加载到viewControllers

Yes that's the way that it works when you're loading xibs that aren't parents to viewControllers

编辑2013年8月15日:

Edit August 15, 2013:

只是假设你要得到你正在寻找的返回的NSArray的索引0,这是什么使得使用类型检查是好的。

You can't always just assume that you're going to get exactly what you're looking for out of index 0 of the returned NSArray, which is what makes it good to use type checking.

NSArray *xibArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:nil options:nil];
MyCustomView *myView = nil;
for (id xibObject in xibArray) {
//Loop through array, check for the object we're interested in.
    if ([xibObject isKindOfClass:[MyCustomView class]]) {
        //Use casting to cast (id) to (MyCustomView *)
        myView = (MyCustomView *)xibObject;
    }
}

这篇关于如何一起使用xib和一个UIView子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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