无法从textField转到另一个视图控制器

时间:2017-06-09 23:10:30

标签: ios swift

我希望从用户在文本字段中输入文本的主视图中进行非常简单的转换,然后将其segue转到下一个视图控制器。我确实实现了函数 textFieldShouldReturn(textField:UITextField) - > Bool 试图执行segue,但没有得到理想的结果。 这是代码的一部分

class LogInVC: UIViewController {
    @IBOutlet weak var logInLabel: UILabel!
    @IBOutlet weak var logInField: UITextField!

    override func viewDidLoad() {
        NSLog("LogIn view loaded, setting delegate")

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        NSLog("viewWillAppear: Performing possible queued updates")
        textFieldShouldReturn(textField: logInField)            
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let autoCheckInVC = segue.destination as?
            AutocheckInVC else {
            preconditionFailure("Wrong destination type: \(segue.destination)")
        }

        guard logInField.text != nil else {
            preconditionFailure("Text property should not be nil")
        }

    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.performSegue(withIdentifier: "AutoCheckInVC", sender: self)
        return true
    }   
}

任何有关调试的快速帮助都将非常感激。我已经花了很多时间来解决这个问题,但我还没找到出路!!!

1 个答案:

答案 0 :(得分:1)

您的代码中有一些错误:

  1. 为了能够致电textFieldShouldReturn,您需要继承UITextFieldDelegate并设置textFieldlogInField.delegate = self
  2. 的代表
  3. 请勿从textFieldShouldReturn
  4. 致电viewDidAppear
  5. 使用方法textFieldShouldReturn
  6. 的最新语法
  7. 您收到警告调用此方法的结果未使用,因为它从未被调用,因为您从未使用delegates
  8. 尝试使用此代码代替您的代码:

    class LogInVC: UIViewController, UITextFieldDelegate {
        @IBOutlet weak var logInLabel: UILabel!
        @IBOutlet weak var logInField: UITextField!
    
        override func viewDidLoad() {
            NSLog("LogIn view loaded, setting delegate")
    
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            logInField.delegate = self
        }
    
        override func viewDidAppear(_ animated: Bool) {
            NSLog("viewWillAppear: Performing possible queued updates")
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            guard let autoCheckInVC = segue.destination as?
                ViewController else {
                    preconditionFailure("Wrong destination type: \(segue.destination)")
            }
    
            guard logInField.text != nil else {
                preconditionFailure("Text property should not be nil")
            }
    
        }
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            self.performSegue(withIdentifier: "AutoCheckInVC", sender: self)
            return true
        }   
    }