Objective c 将NSArray对象移动到UITextFields的最有效方法是什么?

Objective c 将NSArray对象移动到UITextFields的最有效方法是什么?,objective-c,nsarray,Objective C,Nsarray,我有一些代码,其中数组中可能有对象,也可能没有对象。。。这是我正在处理的代码: oServices1.text = CustomServicesArray[0]; oServices2.text = CustomServicesArray[1]; oServices3.text = CustomServicesArray[2]; oServices4.text = CustomServicesArray[3]; oS

我有一些代码,其中数组中可能有对象,也可能没有对象。。。这是我正在处理的代码:

        oServices1.text = CustomServicesArray[0];
        oServices2.text = CustomServicesArray[1];
        oServices3.text = CustomServicesArray[2];
        oServices4.text = CustomServicesArray[3];
        oServices5.text = CustomServicesArray[4];
        oServices6.text = CustomServicesArray[5];
        oServices7.text = CustomServicesArray[6];
        oServices8.text = CustomServicesArray[7];
        oServices9.text = CustomServicesArray[8];
        oServices10.text = CustomServicesArray[9];
        oServices11.text = CustomServicesArray[10];
        oServices12.text = CustomServicesArray[11];
        oServices13.text = CustomServicesArray[12];
        oServices14.text = CustomServicesArray[13];
        oServices15.text = CustomServicesArray[14];
        oServices16.text = CustomServicesArray[15];
        oServices17.text = CustomServicesArray[16];
        oServices18.text = CustomServicesArray[17];
        oServices19.text = CustomServicesArray[18];
        oServices20.text = CustomServicesArray[19];
        oServices21.text = CustomServicesArray[20];
        oServices22.text = CustomServicesArray[21];
        oServices23.text = CustomServicesArray[22];

与其检查每个数组对象是否为零,是否有一种方法可以将oServices*xx*.text ui字段放入某种数组中,这样我就可以使用循环?

您可以使用OutletCollection来保存oServices并在其上循环。但是请注意,插座集合没有排序,因此您需要事先对它们进行排序(例如,根据标记条件或位置)


如需订购,请参阅

将UITextFields的
标记
属性设置为数组中相应的序号。tag的默认值为0,因此如果UITextFields的父视图中存在其他视图,则可能需要将
tag
属性设置为ordinal+1。在文本字段的父视图上,可以使用
viewWithTag:
方法检索相应的UITextField。

您知道自反性吗?使用KVC,您可以节省大量代码和时间:

for(int i=1; i<=23; i++) {
    NSString* key= [NSString stringWithFormat: @"oServices%d"i];
    // Remember that variables should start with a lowercase letter
    [[self valueForKey: key] setText: customServicesArray[i-1] ]; 
}

我认为最后一个解决方案更好,因为您避免绑定这么多变量。

如果我使用索引引用OutLable集合中的对象,而相同的索引引用CuffServices数组?@斯波坎DUID,我上次检查时,OutLoad集合中有一个错误,因此,索引不一定反映您想要呈现的数据。请参阅我答案中的链接问题。我刚刚尝试创建OrderedCollection。。。共有24个UITextFields,但当我进行NSLog以查看OrderedCollection中的内容时,我会得到以下信息:ServiceSoutCollection:(“”),那么如何验证OC中的字段是否正确?@spokane dude集合是一个数组,但您只有一个值。看起来好像您还没有将所有的操作系统服务链接到IB中的集合。下面是我所做的:我捕获了每个UITextField,按住CMD键,然后按住CTRL键并将其拖到Connections Inspector,创建OC。我应该用另一种方式吗?以编程的方式做怎么样?可能的重复正是我想要的。。。我忘了字符串连接。。。像冠军一样工作!谢谢大家的建议!SD
// From the UIViewController
for(int i=1; i<=23; i++) {  // Consider defining a constant instead of 23
    [[self.view viewWithTag: i] setText: customServicesArray[i-1] ];
}