【问题标题】:Xcode Obj - C - retrieve user inputed text from Alert Box IOS8Xcode Obj - C - 从 Alert Box IOS 8 检索用户输入的文本
【发布时间】:2015-03-03 12:08:43
【问题描述】:

我有以下代码,允许用户在警报框中输入他们的全名;

//Creates the alert box
            UIAlertController *alertController = [UIAlertController
                                                  alertControllerWithTitle:@"Congratulations"
                                                  message:@"You Have The High Score, Enter Your Name"
                                                  preferredStyle:UIAlertControllerStyleAlert];
            //Adds a text field to the alert box
            [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
            {
              textField.placeholder = NSLocalizedString(@"Enter Full Name", @"Fullname");
            }];

            [self presentViewController:alertController animated:YES completion:nil];
            //Creates a button with actions to perform when clicked
            UIAlertAction *SaveAction = [UIAlertAction
                                         actionWithTitle:NSLocalizedString(@"SAVE",@"Save Action")
                                         style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction *action)
                                         {
                                             //Stores what has been inputted into the NSString Fullname
                                             NSString *Fullname = alertController.textFields.firstObject;
                                             NSLog(@"Name Stored %@",Fullname);


                                            [self performSegueWithIdentifier:@"NoNextSlide" sender:self];

                                         }];

            [alertController addAction:SaveAction];

你会注意到在线;

NSLog(@"Name Stored %@",Fullname);

这将返回以下内容;

2015-03-03 11:54:19.374 Master Game[1864:26681] Name Stored <_UIAlertControllerTextField: 0x7f9cf61ed780; frame = (4 4; 229 16); text = 'Shaun'; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x7f9cf6084bd0>; layer = <CALayer: 0x7f9cf61854b0>>

我如何从那里获取输入的“Shaun”,以便我可以保存它并在将来使用它?

【问题讨论】:

    标签: objective-c xcode object ios8 inputbox


    【解决方案1】:

    你所说的 FullName 实际上并不是一个字符串。这是一个 UITextField。 如果您想要输入的名称,只需从此文本字段中调用文本属性即可。

    UITextField *nameField = alertController.textFields.firstObject;
    NSString *Fullname = nameField.text;
    

    【讨论】:

      【解决方案2】:

      试试这个:

      //Creates the alert box
      UIAlertController *alertController = [UIAlertController
      alertControllerWithTitle:@"Congratulations"
      message:@"You Have The High Score, Enter Your Name"
      preferredStyle:UIAlertControllerStyleAlert];
      NSString *Fullname = @""
      //Adds a text field to the alert box
      [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
      {
      textField.placeholder = NSLocalizedString(@"Enter Full Name", @"Fullname");
      }];
      
      [self presentViewController:alertController animated:YES completion:nil];
      //Creates a button with actions to perform when clicked
      UIAlertAction *SaveAction = [UIAlertAction
      actionWithTitle:NSLocalizedString(@"SAVE",@"Save Action")
      style:UIAlertActionStyleDefault
      handler:^(UIAlertAction *action)
      {
      //Stores what has been inputted into the NSString Fullname
      UITextField * textField = alertController.textFields.firstObject;
      Fullname = textField.text
      NSLog(@"Name Stored %@",Fullname);
      
      
      [self performSegueWithIdentifier:@"NoNextSlide" sender:self];
      
      }];
      
      [alertController addAction:SaveAction];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多