首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AlertView文本输入

使用AlertView文本输入
EN

Stack Overflow用户
提问于 2015-10-25 21:03:56
回答 1查看 43关注 0票数 0

如何使用从AlertView输入的文本?在本例中,我使用AlertView输入一个电话号码,然后存储在textfield.text中(下面的代码)。我想在包含在同一个.m文件中的另一个方法中使用这些数据。如何正确引用其他方法中的输入(电话号码)数据?

代码语言:javascript
复制
- (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);

        }

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-25 21:08:48

你可能需要做两件事中的一件。第一个选项是在类中为您的电话号码设置一个ivar或属性:

代码语言:javascript
复制
@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

或者,您可以让其他方法将电话号码文本作为参数:

代码语言:javascript
复制
- (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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33335068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档