Xcode Obj - C - retrieve user inputed text from Al

2019-01-29 10:58发布

i have the following code which will allow a user to input their full name into an alert box;

//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];

You'll notice on the line;

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

This will return the following;

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>>

How do i get just the inputted 'Shaun' from there so that i can save it and use it in future?

2条回答
姐就是有狂的资本
2楼-- · 2019-01-29 11:32

What you call FullName isn't actually a string. It's a UITextField. If you want the inputed name, juste call the text property from this textfield.

UITextField *nameField = alertController.textFields.firstObject;
NSString *Fullname = nameField.text;
查看更多
Root(大扎)
3楼-- · 2019-01-29 11:51

Try this:

//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];
查看更多
登录 后发表回答