第一个页面 push 到 第二个页面,第二个页面pop回第一个页面带值
第二个页面写法
// 代理
protocol ContactVCDelegate: NSObjectProtocol {
func callBackDelegateFunc(msg: String)
}
// block
typealias contactBlock = (_ str: String)->Void
class ContactVC: BaseViewController {
...
// block
var cBlock: contactBlock?
func initWithBlock(block: contactBlock?) {
cBlock = block
}
// delegate
var delegate:ContactVCDelegate?
func callBackFunc(msg: String) {
if delegate != nil {
delegate?.callBackDelegateFunc(msg: msg)
}
}
...
// 点一个按钮返回上一页
@objc func saveClick() {
// block 传值
if cBlock != nil {
cBlock!("success")
}
// 代理传值
callBackFunc(msg: "1111")
navigationController?.popViewController(animated: true)
}
}
第一个页面代码,获取第二个页面的值
// 实现代理
class ***VC: ContactViewDelegate, ContactVCDelegate {
...
func gotoContactVC() {
let addContactVC = ContactVC()
// 设置delegate
addContactVC.delegate = self
// block 取值
addContactVC.initWithBlock { str in
print(str)
}
navigationController?.pushViewController(addContactVC, animated: true)
}
// delegate 取值
func callBackDelegateFunc(msg: String) {
print(msg)
}
}