2016-09-26 3 views
0

J'ai un alertview qui affiche un champ de texte que l'utilisateur doit entrer comme obligatoire. Le problème est que si l'utilisateur clique sur "Autoriser" sans l'entrer, l'alerte est rejetée. Je n'arrive pas à trouver une façon de montrer à l'utilisateur que c'est obligatoire sans rejeter le alertview.Y at-il un moyen de fournir une validation dans le champ de texte qui est à l'intérieur de alertview en utilisant swift

image

code:

func tableView (tableView: UITableView !, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

print("You selected cell #\(self.empNameArr[indexPath.row])!") 
    let alertController = UIAlertController(title: "OD Authorise", message: "", preferredStyle: UIAlertControllerStyle.Alert) 

    let AUTHORISE = UIAlertAction(title: "AUTHORISE", style: UIAlertActionStyle.Default, handler: { 
     alert -> Void in 

     let firstTextField = alertController.textFields![3] as UITextField 
     print("<><><><><><>",firstTextField.text) 

    }) 

    let DENY = UIAlertAction(title: "DENY", style: UIAlertActionStyle.Default, handler: { 
     (action : UIAlertAction!) -> Void in 

    }) 

    let CANCEL = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Default, handler: { 
     (action : UIAlertAction!) -> Void in 

    }) 


    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in 
     txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11) 
     txtRemarks.text = " Employee Name :\(self.empNameArr[indexPath.row]) " 
     txtRemarks.userInteractionEnabled=false 
     txtRemarks.borderStyle = UITextBorderStyle.None 

    } 

    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in 
     txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11) 
     txtRemarks.text = " From Date :\(self.leavDateArr[indexPath.row]) " 
     txtRemarks.userInteractionEnabled=false 
     txtRemarks.borderStyle = UITextBorderStyle.None 

    } 
    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in 
     txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11) 
     txtRemarks.text = " To Date :\(self.ToDate[indexPath.row]) " 
     txtRemarks.userInteractionEnabled=false 
     txtRemarks.borderStyle = UITextBorderStyle.None 

    } 



    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in 
     txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11) 
     txtRemarks.text = " Leave reason :\(self.Reason[indexPath.row]) " 
     txtRemarks.userInteractionEnabled=false 
     txtRemarks.borderStyle = UITextBorderStyle.None 

    } 


    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in 
     txtRemarks.placeholder = "Enter Your Remarks" 
     txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 15) 
     txtRemarks.userInteractionEnabled=true 
     txtRemarks.borderStyle = UITextBorderStyle.Line 
     txtRemarks.textAlignment = NSTextAlignment.Center 

    } 


    alertController.addAction(AUTHORISE) 
    alertController.addAction(DENY) 
    alertController.addAction(CANCEL) 

    self.presentViewController(alertController, animated: true, completion: nil) 

} 
+2

Vous pouvez d'abord désactiver les boutons et implémenter la méthode de délégation de champs de texte pour vérifier quand activer les boutons. – Sahil

Répondre

1

Définissez votre classe comme UIAlertViewDelegate et définissez le délégué de votre alrtView comme ta classe.

faites ceci

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex == 1) { 
    NSString *name = [alertView textFieldAtIndex:0].text; 
    // Here check for validation. If the text is empty disable button or however you would like to handle it 
} 
} 
+1

Cette question est étiquetée Swift, pas Objective-C. – Moritz

1
class ViewController: UIViewController,UITextFieldDelegate 
    { 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      // Do any additional setup after loading the view, typically from a nib. 
     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 

     var authorizeaction:UIAlertAction? 

     @IBAction func tapBtnaction(sender: AnyObject) 
     { 
      let titleStr = "title" 
      let messageStr = "message" 

      let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert) 

      let placeholderStr = "Enter your Remarks" 

      alert.addTextFieldWithConfigurationHandler({(textField: UITextField) in 
       textField.placeholder = placeholderStr 
       textField.addTarget(self, action: #selector(self.textChanged(_:)), forControlEvents: .EditingChanged) 
      }) 

      let authorize = UIAlertAction(title: "Authorize", style: UIAlertActionStyle.Default, handler: { (_) -> Void in 

      }) 

      let deny = UIAlertAction(title: "Deny", style: UIAlertActionStyle.Default, handler: { (_) -> Void in 
       let textfield = alert.textFields!.first! 

       //Do what you want with the textfield! 
      }) 
      let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in 
       let textfield = alert.textFields!.first! 

       //Do what you want with the textfield! 
      }) 
      alert.addAction(cancel) 
      alert.addAction(authorize) 
      alert.addAction(deny) 

      //self.actionToEnable = action 
      authorizeaction = authorize 
      authorizeaction!.enabled = false 
      self.presentViewController(alert, animated: true, completion: nil) 


     } 
     func textChanged(sender:UITextField) 
     { 

      if sender.text?.characters.count > 0 
     { 
     authorizeaction?.enabled = true 
     } 
     else 
     { 
      authorizeaction?.enabled = false 
     } 

     } 

    } 
Output: 

enter image description here

enter image description here

EDIT:

Si vous ne souhaitez pas activer ou désactiver l'action vous Autorisez peut utiliser bel code de débit.

let authorize = UIAlertAction(title: "Authorize", style: UIAlertActionStyle.Default, handler: { (_) -> Void in 
      if let textfield : UITextField = alert.textFields![0] 
      { 
       if textfield.text?.characters.count == 0 { 
        //empty 

        self.presentViewController(alert, animated: true, completion: { 
         let tempAlert = UIAlertController(title: "Error", message: "Please Enter remarks", preferredStyle: UIAlertControllerStyle.Alert) 
         tempAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { (action) in 

         })) 

         alert.presentViewController(tempAlert, animated: true, completion: nil) 
        }) 

       } else { 
        //authorize 
       } 
      } 


     }) 

Et l'option alternative pour afficher uniquement les messages est Toast. https://github.com/scalessec/Toast

+0

pouvez-vous expliquer ce "#selector (self.textChanged (_ :))" –

+1

@ Jeesson_7 c'est la même chose que la méthode de délégué TextField ** shouldChangeCharactersInRange ** qui donne des événements de changement de texte dans le champ de texte. –

+0

hey merci pour votre effort. Le problème est que j'ai cette vue d'alerte affichée en cliquant sur une rangée de table. J'ai ajouté mon code à ma question maintenant éditée. –