在查看此link
中的答案后,我创建了文本字段如何检查用户是否在阵列中所有已创建的文本字段中输入了一些文本?如果任何文本字段为空,则应返回false,否则返回true。
编辑: 另请参阅此link。我在数组中得到了文本字段。请参阅答案代码末尾的nslog中的答案。
- (IBAction)save:(id)sender {
mutableTextArray = [[NSMutableArray alloc] init];
for(field in self.scroll.subviews)
{
if([field isKindOfClass:[UITextField class]])
{
if([[field text] length] > 0)
{
[mutableTextArray addObject:field.text];
}
}
}
NSLog(@"Save button 2 : %@", mutableTextArray);
[self fetchStrings:mutableTextArray];
}
- (BOOL) isAllTextFieldsValid {
for (UITextField *fields in mutableTextArray) {
// Trim any spaces entered by user.
NSString *txt = [fields.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([txt length]==0) {
//If any of the contained txt fields will have
//blank text this will return NO.
return NO;
}
}
//Else if all txt fields have some text entered it them, return YES
return YES;
}
- (void) fetchStrings:(NSMutableArray *)textArray
{
NSLog(@"Array string = %@", textArray);
if(![self isAllTextFieldsValid])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot proceed" message:@"Please answer all questions" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
else
{
答案 0 :(得分:0)
如果你有一个uitextfield数组,只需使用for循环并检查数组中的所有对象:
if ([self.textField.text length] > 0 || [self.textField.text isEqualToString:@""] == FALSE){
//textfield has text
}
答案 1 :(得分:0)
我认为您需要做的是编写一个带有for
循环的简单函数来迭代这样的文本字段数组,
- (BOOL) isAllTextFieldsValid {
for (UITextField *field in self.myTextFiledArray) {
// Trim any spaces entered by user.
NSString *txt = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([txt length]==0) {
//If any of the contained txt fields will have
//blank text this will return NO.
return NO;
}
}
//Else if all txt fields have some text entered it them, return YES
return YES;
}
将此功能称为,
if([object isAllTextFieldsValid]) {
//All txt fields have text entered
}
else {
//Show alert to user for filling all text fields,etc.
}
希望有所帮助!
答案 2 :(得分:0)
我刚刚用容量初始化数组并将其与全局整数'i'进行比较,其中'i'具有动态创建的文本fild的计数。
如果我不提及initWithCapacity:0
,它将无效。疯狂。
mutableTextArray = [[NSMutableArray alloc] initWithCapacity:0];
for(field in self.scroll.subviews)
{
if([field isKindOfClass:[UITextField class]])
{
if([[field text] length] > 0)
{
[mutableTextArray addObject:field.text];
}
}
}
NSLog(@"Save button 2 : %@", mutableTextArray);
if([mutableTextArray count] != i)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot proceed" message:@"Please answer all questions" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return;
}
[self fetchStrings:mutableTextArray];