【问题标题】:How do I store text user enters into pop-up text-field into a NSString?如何将用户输入到弹出文本字段中的文本存储到 NSString 中?
【发布时间】:2014-08-15 18:50:01
【问题描述】:

我的一些ViewController.h

NSString *title1;
NSString *sub1;
@interface MapMain : UIViewController
{
    IBOutlet UIButton *setter;
}

都在同一个ViewController.m

-(IBAction)Write:(id)sender
{
    [self alertView];
}

-(void)alertView
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@""
                                                 message:@"Your Info"
                                                 delegate:self
                                                 cancelButtonTitle:@"Cancel"
                                                 otherButtonTitles:@"Done", nil];


    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    UITextField * alertTextField1 = [alert textFieldAtIndex:0];
    alertTextField1.keyboardType = UIKeyboardTypeDefault;
    alertTextField1.placeholder = @"Name";

    UITextField * alertTextField2 = [alert textFieldAtIndex:1];
    alertTextField2.keyboardType = UIKeyboardTypeDefault;
    alertTextField2.placeholder = @"Username";
    alertTextField2.secureTextEntry = NO;

    [alert show];
}

因此,当我单击特定按钮时,此代码正确地使弹出窗口出现两个文本字段。如何将用户在按下“完成”按钮时输入的文本分别存储到我的NSString *title1NSString *sub1 中,以便以后按下其他按钮时可以使用字符串值。当用户按下“取消”按钮时,如何让我的程序不存储文本?

【问题讨论】:

    标签: ios nsstring uitextfield uialertview


    【解决方案1】:

    使您的MapMain 类符合UIAlertViewDelegate 协议

    @interface MapMain : UIViewController <UIAlertViewDelegate>
    

    然后在像这样解除警报时实现委托的方法

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 1) //only if cancel is not clicked (done is clicked)
        {
             UITextField * alertTextField1 = [alertView textFieldAtIndex:0];
             UITextField * alertTextField2 = [alertView textFieldAtIndex:1];
             //... do whatever else you need to here like
             title1 = alertTextField1.text;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 2020-07-09
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      相关资源
      最近更新 更多