Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get textFields from static cells in UITableViewController? Swift

My View hierarchy looks like this:
ElevethViewController of type UIViewController
Container View
ManagedTableEleventhViewController of type UITableViewController embedded in Container View

ManagedTableEleventhViewController contains 4 static cells containing 1 textField each and one empty static cell.

   class ManagedTableEleventhViewController: UITableViewController,UITextFieldDelegate {

  var hasText:Bool! 
  @IBOutlet weak var fullName: UITextField!
  @IBOutlet weak var flatNumber: UITextField!
  @IBOutlet weak var streetAddress: UITextField!
  @IBOutlet weak var phoneNumber: UITextField!

   //checkValue takes ELViewController parameter so that segue can be 
     //performed when button is touched in EleventhViewController


   func checkValue(ELViewController:EleventhViewController) {

     //loop through the textfields and check if they have text
       for case let textField as UITextField in viewController.view.subviews {

      //print is not executed meaning loop is not performed
             print("some text")
         if textField.text == "" {
             self.hasText = false
               textField.layer.borderColor = UIColor.red.cgColor
                 } else {
             print("true value in for loop")
              self.hasText = true

    performSegue(withIdentifier: "elevethToTwelveth", sender: ELViewController)
       }
  }//end of for loop
}





class EleventhViewController: UIViewController {

    var nextButtonOutlet:UIButton!

      override func viewDidLoad() {
            super.viewDidLoad()

//create button programmatically
      var button = UIButton(type: UIButtonType.custom) as UIButton
         button = UIButton(frame: CGRect(x: 0, y: 637, width: 375, height: 50))
          button.titleLabel?.textColor = UIColor.white
          button.backgroundColor = UIColor(colorLiteralRed: 117/255, green: 232/255, blue: 0, alpha: 1)
          button.setTitle("Next", for: .normal)
             button.addTarget(self, action: #selector(EleventhViewController.nextButton), for: .touchUpInside)
         self.view.addSubview(button)
          self.nextButtonOutlet = button
   }


       func nextButton(sender: UIButton) {

      //create instance of tableView
         let managedTable = ManagedTableEleventhViewController()
           managedTable.checkValue(viewController: self)

  } //end of  EleventhViewController class

Main.storyboard

like image 400
bibscy Avatar asked Mar 17 '26 05:03

bibscy


1 Answers

Well first I can give you an answer that might satisfy you and fix your loop but I would recommend not doing it that way to alter your textfields. I would recommend doing it in cellForRow even though they may be static cells. Depending on your view setup in the cells it would look like this if the textfield is added directly to the cells and not to another view.

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("Testing")
    for cell in tableView.visibleCells{
        for sub in cell.contentView.subviews{
            if sub is UITextField{
                print("Textfield")
            }
        }
    }
}
like image 54
agibson007 Avatar answered Mar 19 '26 19:03

agibson007