简体   繁体   English

以编程方式设置参考出口

[英]Set referencing outlets programmatically

我有一个包含三行的视图,并希望以编程方式为它们设置引用出口-如何完成?

IBOutlet is just a marker so Interface Builder knows which instance variables are intended to be assigned objects from your nibs. IBOutlet只是一个标记,因此Interface Builder可以从笔尖知道要为哪些实例变量分配对象。 It doesn't really make sense to talk about assigning outlets automatically - their entire purpose is for the opposite approach. 谈论自动分配出口真的没有意义-它们的整个目的是相反的方法。 The IB stands for Interface Builder. IB代表Interface Builder。

If you want to assign objects in a nib to instance variables, you'll need to step over the view hierarchy and have some way of identifying the objects you want to collect. 如果要在笔尖中将对象分配给实例变量,则需要遍历视图层次结构,并采用某种方式来标识要收集的对象。

You can step over the view hierarchy by accessing self.view in a view controller. 您可以通过在视图控制器中访问self.view来遍历视图层次结构。 If there are objects in the nib that aren't subviews of the main view, you won't be able to access them. 如果笔尖中的对象不是主视图的子视图,则将无法访问它们。 You could load the nib with UINib instead of relying on the built-in functionality to get these objects. 您可以使用UINib加载笔尖,而不是依靠内置功能来获取这些对象。

You can identify the objects in several different ways depending on your needs. 您可以根据需要以几种不同的方式标识对象。 You can assign integers to a view's tag property in Interface Builder. 您可以在Interface Builder中将整数分配给视图的tag属性。 You can look at the views that you know will be at a certain point in the hierarchy. 您可以查看您知道将在层次结构中某个点上的视图。 You can look at views of a particular type. 您可以查看特定类型的视图。 Or you can mix and match - for example, the UIButton subclass of the main view with an even tag. 或者,您可以混合搭配-例如,主视图的UIButton子类带有偶数标签。

If you are laying out your views with Interface Builder, it is almost always easiest to simply assign the views to outlets with Interface Builder instead. 如果要使用Interface Builder布置视图,则通常总是最简单的方法是使用Interface Builder将视图分配给出口。

(any pointer) = [[self.view subviews] objectAtIndex:(object's index)];

dont sure about Object's ID. 不确定对象的ID。 try to find in identity inspector of interface builder 尝试在界面生成器的身份检查器中查找

If you want your table view cells of one view ordered from left to right you could do the following. 如果要使一个视图的表格视图单元格从左到右排序,则可以执行以下操作。

NSMutableArray *myCells = [[NSMutableArray alloc] initWithCapacity:NUMBER_OF_CELLS];
NSArray *allSubviews = [theView subviews];

for(NSView *aSub in allSubviews) {
    if([aSub isKindOfClass:[MyCustomCellViewClass class]]) {
        [myCells addObject:aSub];
    }
}

// Here you can sort your found cells with their origin's x values.
// -[NSMutableArray sortUsingFunction:context:] can help you with that.
// Do what you want to do
[myCells release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM