如何使用从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);
}
}
}发布于 2015-10-25 21:08:48
你可能需要做两件事中的一件。第一个选项是在类中为您的电话号码设置一个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);
}https://stackoverflow.com/questions/33335068
复制相似问题