如何从xib父视图获取子视图

时间:2015-02-25 00:03:46

标签: ios iphone uiview xib

我有一个带有单个UIButton的xib文件。所以基本上它是一个内部有UIButton的UIView。给xib充气后,如何处理UIButton?

注意:我的ViewController本身是使用StoryBoard创建的。我只需要这个额外的按钮作为我的UITableView的页脚。我希望按钮位于页脚中心。因此,不是以编程方式创建按钮(我不知道如何告诉它居中),而是如上所述在xib中创建它。

2 个答案:

答案 0 :(得分:0)

您必须创建一个MyCustomButton类,该类将在UIView之后继承。

然后,您将按钮view中的xib设为MyCustomButton类型。

enter image description here

然后,您可以在IBOutlet课程中为该按钮创建MyCustomButton

现在,您的自定义xib由自定义类MyCustomButton描述,该类具有对该按钮的引用。

更详细地阅读here

答案 1 :(得分:0)

这些被称为顶级对象,您可以使用以下代码访问它们。

NSNib *nib = [[[NSNib alloc] initWithNibNamed:@"MyView" bundle:nil] autorelease];
NSArray *topLevelObjects;
if (! [nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) // error

MyView *myView = nil;
for (id topLevelObject in topLevelObjects) {
if ([topLevelObject isKindOfClass:[MyView class]) {
    mySelectedView = topLevelObject;
    break;
    }
}

在if语句中选择你想要处理的正确类,你将在“mySelectedView”中获得该对象。