获得以下代码:
-(void)addGrade {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Text" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
// Textfield1
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[utextfield becomeFirstResponder];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
utextfield.leftView = paddingView;
utextfield.leftViewMode = UITextFieldViewModeAlways;
utextfield.placeholder = @"Subject";
[utextfield setBackgroundColor:[UIColor whiteColor]];
utextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[alertview addSubview:utextfield];
// Textfield2
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 90.0, 25.0)];
ptextfield.placeholder = @"Another Subject";
ptextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
ptextfield.textAlignment = NSTextAlignmentCenter;
[ptextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:ptextfield];
// Textfield3
UITextField *ctextfield = [[UITextField alloc] initWithFrame:CGRectMake(161.0, 85.0, 27.0, 25.0)];
ctextfield.placeholder = @"3";
[ctextfield setBackgroundColor:[UIColor whiteColor]];
ctextfield.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
ctextfield.textAlignment = NSTextAlignmentCenter;
[alertview addSubview:ctextfield];
// Label1
UILabel *telt = [[UILabel alloc] initWithFrame:CGRectMake(121.0, 85.0, 40.0, 25.0)];
telt.text = @"Text";
telt.textColor = [UIColor whiteColor];
telt.backgroundColor = [UIColor clearColor];
[alertview addSubview:telt];
// Label2
UILabel *keermee = [[UILabel alloc] initWithFrame:CGRectMake(199.0, 85.0, 100.0, 25.0)];
keermee.text = @"more text";
keermee.textColor = [UIColor whiteColor];
keermee.backgroundColor = [UIColor clearColor];
[alertview addSubview:keermee];
[alertview show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"Add"])
{
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (@"%@", field.text);
}
}
此代码在UIAlertView中生成3个Textfields和2个标签。
使用UITextField *field = (UITextField *)[[alertView subviews] lastObject];
我得到了“更多文本”这是真的,因为你问的是最后一个对象。
现在我的问题是如何获取所有UITextfield的内容不仅仅是标签?
答案 0 :(得分:1)
for (UIView *view in [alertView subviews]){
if ([view isMemberOfClass:[UITextField class]]){
UITextField *textField = (UITextField *)view;
NSLog(@"%@", textField.text);
}
}
答案 1 :(得分:1)
分配标签并使用它们检索标签。
myTextField.tag = 100;
然后找到它使用这个功能:
myTextField = [view subviewWithTag: 100];