将视图控制器关闭到以前的视图控制器

时间:2018-04-26 10:38:23

标签: ios swift swift-protocols

我知道很多人可能会问这个问题。我也找到了解决方案,但我无法理解我的代码中出了什么问题?

popcontroller.swift:

protocol DataSendDelegate {
    func sendgetattid(pop_att_id: Int, pop_in_time: String)
}
var delegate: DataSendDelegate? = nil

if statuss == "200" {
let send_pop_att_id = att_id
let send_pop_in_time = in_time
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true) {                                    
self.delegate?.sendgetattid(pop_att_id: send_pop_att_id, pop_in_time: send_pop_in_time)
}
}

在上面的代码中,我得到了正确的值,但它没有被忽略并转到下一个视图控制器。

attendanceViewController.swift:

class attendanceViewController: UIViewController , DataSendDelegate {
func sendgetattid(pop_att_id: Int, pop_in_time: String) {
        DispatchQueue.main.async {
            self.inTimeTextField.text = pop_in_time
            self.get_att_id = pop_att_id
            self.in_time_button.isEnabled = false
            self.out_time_button.isEnabled = true
        }
    }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let popVC = segue.destination as? PopUpViewController
        {
            popVC.popEmailID = att_emp_id
            popVC.delegate = self
        }
    }
}

2 个答案:

答案 0 :(得分:1)

从评论中更新:  你没有提出viewController。你推了一些视图控制器。所以只要弹出它。丢弃dismiss

self.delegate?.sendgetattid(pop_att_id: send_pop_att_id, pop_in_time: send_pop_in_time)
self.navigationController?.popViewController(animated: true)

答案 1 :(得分:0)

如果你从前一个ViewController中做self.present(ViewController, animated: true, completion: nil)来显示当前的viewcontroller,你必须这样做

self.dismiss(animated: true) {                                    
      self.delegate?.sendgetattid(pop_att_id: send_pop_att_id, pop_in_time: send_pop_in_time)
}

或者如果您self.navigationController?.pushViewController(ViewController, animated: true),则必须执行self.navigationController?.popViewController(animated: true)