3

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

bibscy
  • 2,598
  • 4
  • 34
  • 82
  • What is viewController? The code you pasted is fractured. – Magnas Mar 03 '17 at 20:40
  • @Magnas View Controller is EleventhViewController . I have created the view hierarchy after this tutorial https://www.youtube.com/watch?v=zAWO9rldyUE – bibscy Mar 03 '17 at 20:42
  • In the code you posted, the ManagedTableEleventhViewController class has no methods. Is that just a 'pasting' decision by you or are there really no methods in the class? – Magnas Mar 03 '17 at 20:50
  • @Magnas I wanted to minimize the amount of code pasted so that it could be more readable. I have updated my code with the programmatic button which calls `func nextButton()` every time it is touched. – bibscy Mar 03 '17 at 20:55
  • @Magnas I have misread your question. In ManagedTableEleventhViewControlle there is `func checkValue(ELViewController:EleventhViewController) ` method . Now, checkValue method is called in EleventhViewController every time nextButtonOutlet is touched. – bibscy Mar 03 '17 at 20:57
  • I think the problem is when you call checkValue you pass in the EleventhViewController (when you pass self) which has no textFields. The textFields are in your tableViewController, so your loop looks for all of the textFields in EleventhViewController but there are none so the print statement doesn't get called. – Magnas Mar 03 '17 at 21:03
  • @Magnas I don't think that is the issue. If you do `func checkValue() {...some code here}`with no parameters and then call it in viewDidAppear, it still does not print any output for `for case let textField as UITextField in self.view.subviews` – bibscy Mar 03 '17 at 21:13
  • @Magnas could you please have a look at my code as I am getting an error when assigning self as the delegate for the text fields http://stackoverflow.com/questions/42600534 – bibscy Mar 04 '17 at 19:47

2 Answers2

3

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")
            }
        }
    }
}
agibson007
  • 4,173
  • 2
  • 19
  • 24
  • the textfields are dragged from Object library and dropped in the static cells of the tableView. – bibscy Mar 04 '17 at 14:21
1

Just to follow up, if this is for validation you should'nt be only checking "" case, because you allow " ", " " etc. Use isEmpty, it should work better, if you only want to check existence of text. Also you dont have to extract fields from subviews as you already have properties, i'm not sure if you have any other reason for this logic though.

Edit. Ooops, i just noticed your checking for textfields in a controller which does not have any visible fields, so normally your check never passes.

I think you should'nt even validate textfields for one class in another class, unless its a class handling textfield validation in general.

In EleventhViewController you have no textfields, so nothing to validate.

silentBob
  • 170
  • 9