如何使用从AlertView输入的文本?在这个例子中,我使用AlertView输入一个电话号码,然后存储在textfield.text(下面的代码)中。我想在同一.m文件中包含的另一个方法中使用此数据。如何从另一种方法中正确引用此输入(电话号码)数据?
- (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Capture the phone number input from the alert pop-up window. UIAlertView Delegate added to allow the OS trigger this method to read the data.
if (alertView.tag == 12) {
if (buttonIndex == 1) {
UITextField *textfield = [alertView textFieldAtIndex:0];
NSLog(@"phonenumber: %@", textfield.text);
}
}
}
答案 0 :(得分:0)
你可能需要做两件事之一。第一个选项是为您的班级中的电话号码设置ivar或属性:
@implementation SomeViewController {
NSString* _phoneNumber;
}
- (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex (NSInteger)buttonIndex
{
// Capture the phone number input from the alert pop-up window. UIAlertView Delegate added to allow the OS trigger this method to read the data.
if (alertView.tag == 12) {
if (buttonIndex == 1) {
UITextField *textfield = [alertView textFieldAtIndex:0];
_phoneNumber = textField.text;
}
}
}
- (void)someOtherMethod {
NSLog(@"phonenumber: %@", _phoneNumber);
}
@end
或者,您可以使用其他方法将电话号码文本作为参数:
- (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex (NSInteger)buttonIndex
{
// Capture the phone number input from the alert pop-up window. UIAlertView Delegate added to allow the OS trigger this method to read the data.
if (alertView.tag == 12) {
if (buttonIndex == 1) {
UITextField *textfield = [alertView textFieldAtIndex:0];
[self someOtherMethodThatHandlesAPhoneNumber:textField.text];
}
}
}
- (void)someOtherMethodThatHandlesAPhoneNumber:(NSString*)phoneNumber {
NSLog(@"phonenumber: %@", phoneNumber);
}