What's a simple way to get a text input popup dialog box on an iPhone

后端 未结 12 2051
-上瘾入骨i
-上瘾入骨i 2020-12-02 03:42

I want to get the user name. A simple text input dialog box. Any simple way to do this?

相关标签:
12条回答
  • 2020-12-02 04:34

    Building on John Riselvato's answer, to retrieve the string back from the UIAlertView...

    alert.addAction(UIAlertAction(title: "Submit", style: UIAlertAction.Style.default) { (action : UIAlertAction) in
                guard let message = alert.textFields?.first?.text else {
                    return
                }
                // Text Field Response Handling Here
            })
    
    0 讨论(0)
  • 2020-12-02 04:35

    I would use a UIAlertView with a UITextField subview. You can either add the text field manually or, in iOS 5, use one of the new methods.

    0 讨论(0)
  • 2020-12-02 04:36

    Just wanted to add an important piece of information that I believe was left out perhaps with the assumption that the ones seeking answers might already know. This problem happens a lot and I too found myself stuck when I tried to implement the viewAlert method for the buttons of the UIAlertView message. To do this you need to 1st add the delegate class which may look something like this:

    @interface YourViewController : UIViewController <UIAlertViewDelegate>

    Also you can find a very helpful tutorial here!

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 04:37

    To make sure you get the call backs after the user enters text, set the delegate inside the configuration handler. textField.delegate = self

    Swift 3 & 4 (iOS 10 - 11):

    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
    alert.addTextField(configurationHandler: {(textField: UITextField!) in
        textField.placeholder = "Enter text:"
        textField.isSecureTextEntry = true // for password input
    })
    self.present(alert, animated: true, completion: nil)
    

    In Swift (iOS 8-10):

    enter image description here

    override func viewDidAppear(animated: Bool) {
        var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
        alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
            textField.placeholder = "Enter text:"
            textField.secureTextEntry = true
            })
        self.presentViewController(alert, animated: true, completion: nil)
    }
    

    In Objective-C (iOS 8):

    - (void) viewDidLoad 
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"Click" style:UIAlertActionStyleDefault handler:nil]];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Enter text:";
            textField.secureTextEntry = YES;
        }];
        [self presentViewController:alert animated:YES completion:nil];
    }
    

    FOR iOS 5-7:

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"INPUT BELOW" delegate:self cancelButtonTitle:@"Hide" otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
    

    enter image description here


    NOTE: Below doesn't work with iOS 7 (iOS 4 - 6 Works)

    Just to add another version.

    UIAlert With UITextField

    - (void)viewDidLoad{
    
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Preset Saving..." message:@"Describe the Preset\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        UITextField *textField = [[UITextField alloc] init];
        [textField setBackgroundColor:[UIColor whiteColor]];
        textField.delegate = self;
        textField.borderStyle = UITextBorderStyleLine;
        textField.frame = CGRectMake(15, 75, 255, 30);
        textField.placeholder = @"Preset Name";
        textField.keyboardAppearance = UIKeyboardAppearanceAlert;
        [textField becomeFirstResponder];
        [alert addSubview:textField];
    
    }
    

    then I call [alert show]; when I want it.

    The method that goes along

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {         
        NSString* detailString = textField.text;
        NSLog(@"String is: %@", detailString); //Put it on the debugger
        if ([textField.text length] <= 0 || buttonIndex == 0){ 
            return; //If cancel or 0 length string the string doesn't matter
        }
        if (buttonIndex == 1) {
            ...
    
        }
    }
    

    0 讨论(0)
  • 2020-12-02 04:38
    UIAlertview *alt = [[UIAlertView alloc]initWithTitle:@"\n\n\n" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(25,17, 100, 30)];
    lbl1.text=@"User Name";
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(25, 60, 80, 30)];
    lbl2.text = @"Password";
    
    UITextField *username=[[UITextField alloc]initWithFrame:CGRectMake(130, 17, 130, 30)];
    UITextField *password=[[UITextField alloc]initWithFrame:CGRectMake(130, 60, 130, 30)];
    
    lbl1.textColor = [UIColor whiteColor];
    lbl2.textColor = [UIColor whiteColor];
    
    [lbl1 setBackgroundColor:[UIColor clearColor]];
    [lbl2 setBackgroundColor:[UIColor clearColor]];
    
    username.borderStyle = UITextBorderStyleRoundedRect;
    password.borderStyle = UITextBorderStyleRoundedRect;
    
    [alt addSubview:lbl1];
    [alt addSubview:lbl2];
    [alt addSubview:username];
    [alt addSubview:password];
    
    [alt show];
    
    0 讨论(0)
  • 2020-12-02 04:44

    Swift 3:

    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
    alert.addTextField(configurationHandler: {(textField: UITextField!) in
         textField.placeholder = "Enter text:"
    })
    
    self.present(alert, animated: true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题