Reputation: 75
I have button set up, and when it gets clicked, an alertview will appear with a text field. I want to get the data from that variable and trigger an addition function which adds the value of the text field to a local variable and displays the data in a label. How can I do that?
Upvotes: 2
Views: 401
Reputation: 125007
You need to set a delegate for the alert view, and that object should adopt the UIAlertViewDelegate protocol. The delegate will then receive a -alertView:willDismissWithButtonIndex:
message before the alert is dismissed. Your delegate's implementation of that method can get the text from whatever text fields are in the alert -- use -textFieldAtIndex:
to get the different text fields.
Upvotes: 0
Reputation: 3211
I'm assuming you've already got the UIAlertView with a UITextField (either using the UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput).
I'm also going to assume that you've set up the UIAlertViewDelegate so that it gets called properly when a button on the UIAlertView is pressed. So, to get the value of the UITextField, in your alertView:clickedButtonAtIndex:
you add the following code (assuming only one UITextField - if you have multiple, just change the parameter passed to textFieldAtIndex:
NSInteger textFieldValueAsInteger = [[alertView textFieldAtIndex:0].text integerValue];
NSInteger finalValue = textFieldValueAsInteger + someOtherIntegerThatYouAlreadyDeclared;
Displaying it largely depends on your your UILabel etc are set up, but basically:
UILabel *labelToDisplayResults = [[UILabel alloc] initWithFrame:self.view.frame];
[self.view addSubview:labelToDisplayResults];
[labelToDisplayResults setText:[NSString stringWithFormat:@"%d", finalValue]];
That should do it. Let me know if you have any other follow-up questions.
Upvotes: 0
Reputation: 437622
Set the delegate
property of UIAlertView
and have that object conform to UIAlertViewDelegate
. Namely, respond to alertView:clickedButtonAtIndex:
, during which you can inquire regarding textFieldAtIndex
.
Upvotes: 1