2016-09-26 129 views
0

我有一個alertview,顯示用戶必須輸入的文本字段。問題是如果用戶點擊「授權」而不輸入它,那麼alertview將被解僱。我無法找到一種向用戶顯示它是強制性的而不關閉alertview的方式。有沒有一種方法可以在使用swift的alertview中提供驗證?

image

代碼:

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

您可以先禁用按鈕,然後執行textfield委託方法來檢查何時啓用按鈕。 – Sahil

回答

1

設置你的類作爲UIAlertViewDelegate並設置alrtView的委託作爲你的班。

那麼做到這一點

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

這個問題被標記爲Swift,而不是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

編輯:

如果您不希望啓用或禁用授權的行動,那麼你可以使用bel ow代碼。

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


     }) 

和備選選項來顯示信息僅是麪包。 https://github.com/scalessec/Toast

+0

可以解釋這個「#selector(self.textChanged(_ :))」 –

+1

@ Jeesson_7它與TextField委託方法** shouldChangeCharactersInRange **相同​​,它給出文本字段中文本更改的事件。 –

+0

嘿謝謝你的努力。問題是我在點擊表格行時顯示了這個警報視圖。我已將我的代碼添加到我現在編輯的問題中。 –

相關問題