How to pass data back to ViewController holding Container View and TableViewController that is embedded in the Container? Swift 3

前端 未结 1 1511
说谎
说谎 2021-01-29 13:41

How can I call func checkField when nextButton is tapped? If no field is empty in ManagedTableEleventhViewController, I\'d like to segue to TwelvethViewController.

I hav

1条回答
  •  时光说笑
    2021-01-29 14:04

    Thanks to this answer I have obtained the desired result.

    class EleventhViewController: UIViewController {
    
    @IBAction func nextButton(_ sender: Any) {
     var managed: ManagedTable? {
        return self.childViewControllers.last as! ManagedTable?
       //if there is no empty field after checkField(sender:_) is called,    
        // segue to TwelvethViewController
       managed?.checkField(sender: self)
        } 
      }
    }
    
    
    
     class ManagedTableEleventhViewController {
    
     // holds a true value if textField contains a value
      var hasText:Bool!
    
     //holds a true/false value for each textField after iterating through them    
          //with a for loop
    var textValues = [Bool]()
    
    //When nextButton is touched in EleventhViewController, call this method
    func checkField(sender:EleventhViewController) {
    
    
        for cell in self.tableView.visibleCells{
            for sub in cell.contentView.subviews{
    
                if sub is UITextField{
    
    let textF = sub as? UITextField
             //if textField is empty, make its border red,else clear
                    if textF?.text == "" {
                        self.hasText = false
                        self.textValues.append(self.hasText)
                        textF?.layer.cornerRadius = 8.0
                        textF?.layer.masksToBounds = true
                        textF?.layer.borderColor = UIColor.red.cgColor
                        textF?.layer.borderWidth = 1.0
                    } else {
                        self.hasText = true
                        self.textValues.append(self.hasText)
    
                    }
                }// end of if sub is UITextField
    
            }//end of for sub in cell.contentView
    
        } //end of  for cell in tableView
    
       //if no field is empty perform segue to TwelvethViewController, 
           //otherwise reset textValues array
          if !self.textValues.contains(false){
             sender.performSegue(withIdentifier: "eleventhToTwelveth", sender: sender)
            }
         self.textValues.removeAll()
        } //end of checkField(sender:_)
    }//end of class
    

    0 讨论(0)
提交回复
热议问题