Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add and access TextField in UIAlertController ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 477
    Comment on it

    The following code will show the Alert with textField :-

    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Alert Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        // to add textField
        
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
            
        //  do any textField related code here
            
     }];
    

    We need to dismiss the alert also.. So for that we will add a action to the UIAlert to dismiss it.. Here, its shows two buttons OK and CANCEL added to the UIAlert.. The code is :-

    UIAlertAction *updateAction=[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
            UITextField *txtField = [alert.textFields firstObject];
            NSLog(@"Your text %@",txtField.text);  // to access the text field value
            
        }];
        
    UIAlertAction *dismissAction=[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction  *_Nonnull action) {
            
        }];
        
        [alert addAction:updateAction];
        [alert addAction:dismissAction];
    

    Its working !!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: