Reputation: 21
I have got an alertview which has a text field
UIAlertView* dialog = [[UIAlertView alloc] init];
dialog.tag = 5;
[dialog setDelegate:self];
dialog.delegate = self;
[dialog setTitle:@"Please set the quantity:"];
[dialog setMessage:@"Quantity"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"SET"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
//nameField.keyboardType = UIKeyboardTypeDecimalPad;
NSString *str = nameField.text;
NSLog(@"%@",str);
[dialog addSubview:nameField];
[dialog show];
[dialog release];
[nameField release];
I'm trying to store the values of the textfield after the alertviews dismissal as
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
UITextField* textField = (UITextField*)[alertView.subviews objectAtIndex:2];
NSLog(@"%@",textField.text);
}
}
}
But the text field is returning null, what is the value that im supposed to give or objectatindex?
Thanks.
Upvotes: 0
Views: 766
Reputation: 547
Try this
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
//UITextField* textField = (UITextField*)[alertView.subviews objectAtIndex:2];
//NSLog(@"%@",textField.text);
for (UIView *subview in [alertView subviews])
{
if ([subview isKindOfClass:[UITextField class]])
{
UITextField * textField =(UITextField *)subview;
NSLog(@"%@",textField.text);
}
}
}
}
}
Upvotes: 1
Reputation: 15213
Use the method instance method textFieldAtIndex:
of UIAlertView
.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
UITextField* textField = [alertView.subviews textFieldAtIndex:2]; //make sure the index of the text field is correct.
NSLog(@"%@",textField.text);
}
}
}
NOTE: This method is available in iOS 5+
Upvotes: 0