How to add a TextField to UIAlertView in Swift

前端 未结 10 937
耶瑟儿~
耶瑟儿~ 2020-12-23 13:41

I have this code, but I dont know how to show a textfield inside the UIAlertView.

var altMessage = UIAlertController(title: \"Warning\", message: \"This is A         


        
相关标签:
10条回答
  • 2020-12-23 14:31

    You can access the textfield with:

    let textField = alert.textFieldAtIndex(0)
    

    Then to change the placeholder text:

    textField.placeholder = "Foo!"
    

    And the keyboard type:

    textField.keyboardType = ...
    
    0 讨论(0)
  • 2020-12-23 14:31
                var inputTextField: UITextField?
    
                //Create the AlertController
                let actionSheetController: UIAlertController = UIAlertController(title: "Rename", message: "", preferredStyle: .Alert)
    
                //Create and add the Cancel action
                let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            //Do some stuff
                }
                actionSheetController.addAction(cancelAction)
                //Create and an option action
                let nextAction: UIAlertAction = UIAlertAction(title: "OK", style: .Default) { action -> Void in
            //Do some other stuff
                }
                actionSheetController.addAction(nextAction)
                //Add a text field
                actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
            // you can use this text field
            inputTextField = textField
                }
    
                //Present the AlertController
                self.presentViewController(actionSheetController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-23 14:33

    Swift 4:

    var textField: UITextField?
    
    func configurationTextField(textField: UITextField!) {
        if (textField) != nil {
            self.textField = textField!        //Save reference to the UITextField
            self.textField?.placeholder = "Some text";
        }
    }
    
    func openAlertView() {
        let alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addTextField(configurationHandler: configurationTextField)
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler:{ (UIAlertAction) in
            print("User click Ok button")
        }))
        self.present(alert, animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2020-12-23 14:36

    In Objective C

     UIAlertView *alertView =  [[UIAlertView alloc]initWithTitle:@"Duplicate file" message:@"A file with the same name already exists." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
     alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    
     [[alertView textFieldAtIndex:0] setText:@"Filename"];
     [[alertView textFieldAtIndex:0] setPlaceholder:@"Enter Filename"];
     [alertView show];
    

    In Swift 2.3

    func doSomething(){
        var alert = UIAlertController(title: "Duplicate file", message: "A file with the same name already exists.", preferredStyle: UIAlertControllerStyle.Alert)
    
        alert.addTextFieldWithConfigurationHandler(configurationTextField)
    
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
             print("User click Ok button")
             print(self.textField.text)
        }))
    
     self.presentViewController(alert, animated: true, completion: {
         print("completion block")
     })
    }
    
     func configurationTextField(textField: UITextField!){
         textField.text = "Filename"
     }
    

    In Swift 3

    func doSomething(){
        var alert = UIAlertController(title: "Duplicate file", message: "A file with the same name already exists.", preferredStyle: UIAlertControllerStyle.alert)
    
        alert.addTextField(configurationHandler: configurationTextField)
    
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
            print("User click Ok button")
            print(self.textField.text)
        }))
    
        self.present(alert, animated: true, completion: {
            print("completion block")
        })
    }
    
    func configurationTextField(textField: UITextField!){
        textField.text = "Filename"
    }
    
    0 讨论(0)
提交回复
热议问题