【问题标题】:How can I create array objects from UITextFields?如何从 UITextFields 创建数组对象?
【发布时间】:2011-10-31 19:26:06
【问题描述】:
如何从 UITextFields 创建数组对象?我还想要每个对象的 if 语句来检查 UITextField 的文本长度是否大于 1。
我怎样才能使用这个核心代码做到这一点?:
maincelltext = [[NSArray alloc] initWithObjects:@"UITextField 1 Content Here",@"UITextField 2 Content Here",@"UITextField 3 Content Here",@"UITextField 3 Content Here",nil];
谢谢!
【问题讨论】:
标签:
iphone
objective-c
ios
xcode
nsarray
【解决方案1】:
改用 NSMutableArray,然后 addObject:if(textField1.text.length > 1) then [yourMutableArray addObject:textField1.text]; 等等...
类似这样的:
// in your interface
UITextField * textField1;
UITextField * textField2;
UITextField * textField3;
NSMutableArray * mainCellTextArray;
//implementation
mainCellTextArray = [[NSMutableArray alloc] init]; // release it later
if(textField1.text.length > 1)
{
[mainCellTextArray addObject:textField1.text];
}
if(textField2.text.length > 1)
{
[mainCellTextArray addObject:textField2.text];
}
if(textField3.text.length > 1)
{
[mainCellTextArray addObject:textField3.text];
}
【解决方案2】:
我将使用 IBOutletCollection 并将所有 UITextFields 添加到其中,而不是创建 NSArray。您可以通过 Interface Builder 轻松完成此操作。无论您使用 NSArray 还是 IBOutletCollection,都可以轻松完成循环并检查每个文本长度是否超过 1。只需使用许多循环结构中的任何一个(即 for、for-in)并检查每个项目的 text 属性的长度。