如何从 UITableViewController 中的静态单元格获取 textFields?迅速 [英] How to get textFields from static cells in UITableViewController? Swift

查看:29
本文介绍了如何从 UITableViewController 中的静态单元格获取 textFields?迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图层次结构如下所示:
类型为 UIViewController
的 ElevethViewController容器视图
嵌入在容器视图中的 UITableViewController 类型的 ManagedTableEleventhViewController

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

ManagedTableEleventhViewController 包含 4 个静态单元格,每个单元格包含 1 个文本字段和一个空的静态单元格.

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

推荐答案

首先我可以给你一个可能让你满意的答案并修复你的循环,但我建议不要这样做来改变你的文本字段.即使它们可能是静态单元格,我也建议在 cellForRow 中执行此操作.根据您在单元格中的视图设置,如果文本字段直接添加到单元格而不是其他视图,则它看起来像这样.

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

这篇关于如何从 UITableViewController 中的静态单元格获取 textFields?迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆