从另一个控制器验证Textfield后,从主控制器启用按钮

时间:2018-01-26 14:29:17

标签: ios swift swift3

我的ViewController上有一个名为 Validate()的函数,在另一个控制器的文本字段后调用此函数 DidEndEditing() 但是当我要启用按钮时,它会给我一个错误找到nil,同时展开可选值 ..

ViewController:

  func validate() {

    var txtboxcount = 0
    var txtboxcountval = 0

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "QuestionsPage") as! QuestionsViewController


    var offer = NeedPaymentViewController.details.offer
    var address = NeedPaymentViewController.details.address



    var qsts = QuestionsViewController.question.qsts
    var total = QuestionsViewController.question.totalqsts

    print("qsts: \(qsts) and total: \(total) and offer: \(offer) and address : \(address)")



    if(offer != "" && address != "" && qsts == total) {
      saveBtn.isEnabled = true --> **Error happens Here "Found Nil" **
      saveBtn.backgroundColor = UIColor.red
    }

另一名控制人:

   func validate() {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController;
    vc.validate()
  }

   @IBAction func offerTxtDoneEditing(_ sender: Any) {
    NeedPaymentViewController.details.offer = ""
    NeedPaymentViewController.details.address = ""

    NeedPaymentViewController.details.offer = offerTxt.text!
    NeedPaymentViewController.details.address = addressTxt.text!

    validate()
}

1 个答案:

答案 0 :(得分:0)

您只是实例化VC,当您调用" vc.validate()"时,它在视图层次结构中不活动,因此您的saveBtn插座尚未连接,从而导致崩溃。

无论如何......你的结构似乎需要一些改变。让viewcontrollers使用委托进行通信,并将viewcontroller非特定逻辑提取到其他类。