在查看此链接中的答案后,我创建了文本字段
如何检查用户是否在数组中所有创建的文本字段中输入了一些文本?如果任何文本字段为空,则应返回 false,否则返回 true。
编辑:另请参阅此链接。我得到数组中的文本字段。参考答案代码末尾的 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
{