Ios swift中ViewController之间的委托实现

Ios swift中ViewController之间的委托实现,ios,swift,delegates,Ios,Swift,Delegates,我有两个视图控制器VCA和VCB。 从VCA移动到VCB,使用一些值,工作正常 let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB vc.entity = somevalue self.navigationController?.pushViewController(vc, animated: true) 但对于反向,我想在从VCB上传某些数据后,从VCB调用VCA中的方法。然后刷新V

我有两个视图控制器
VCA
VCB
。 从
VCA
移动到
VCB
,使用一些值,工作正常

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = somevalue
self.navigationController?.pushViewController(vc, animated: true)
但对于反向,我想在从
VCB
上传某些数据后,从
VCB
调用
VCA
中的方法。然后刷新
VCA
中的文本字段值。我可以让视图中的重新刷新代码出现在
VCA
中,但由于某些原因,我没有这样做,而是尝试实现委托。 我编写了以下代码:

VCA:

class ProfileEditViewController:UIViewControoler, MobileVerifyDelegate{
override func viewDidLoad() {

    super.viewDidLoad()
    let mobileVC = MobileVerificationViewController()
    mobileVC.delegate = self
}

//MARK: - Mobileverify Delegate method
func delegateMethod() {
    print("a")

}
}
    protocol MobileVerifyDelegate{
    func delegateMethod()
}


class MobileVerificationViewController: UIViewController{
 var delegate: MobileVerifyDelegate! = nil
func certainFunction(){
     //aftersuccessful upload
     self?.delegate.delegateMethod()// code crashes
}
}
VCB:

class ProfileEditViewController:UIViewControoler, MobileVerifyDelegate{
override func viewDidLoad() {

    super.viewDidLoad()
    let mobileVC = MobileVerificationViewController()
    mobileVC.delegate = self
}

//MARK: - Mobileverify Delegate method
func delegateMethod() {
    print("a")

}
}
    protocol MobileVerifyDelegate{
    func delegateMethod()
}


class MobileVerificationViewController: UIViewController{
 var delegate: MobileVerifyDelegate! = nil
func certainFunction(){
     //aftersuccessful upload
     self?.delegate.delegateMethod()// code crashes
}
}

提前感谢您在您创建的VCA的
视图didload
中创建了
mobileVC
,但是当您转换到VCB时,您正在创建一个名为
vc
的VCB新实例
mobileVC
未按原样使用。您有几个选择:

在创建
vc
时,将
mobileVC
设置为类属性或设置委托

后者将是:

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = someValue
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
在旁注中,让您的委托确认类协议,以便您可以将委托设置为弱

protocol MobileVerifyDelegate: class {
    func delegateMethod()
}

class MobileVerificationViewController: UIViewController {
    weak var delegate: MobileVerifyDelegate?

    func certainFunction() {

        // After successful upload
        delegate?.delegateMethod()
    }
}
请注意,当您设置隐式展开属性时,它已经是
nil
。因此,再次将其设置为
nil
是多余的

var delegate: MobileVerifyDelegate! = nil // "= nil" is not needed

我不知道您的情况是什么,但最简单的解决方案是将委托方法和委托移动到
VCB
。如果出于任何原因,
VCA
必须是委托类,则需要创建它的实例或将其传递给
VCB

//Set delegate when you push to the new controller in VCA
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = somevalue
vc.delegate = self //Sets VCA as VCB's Delegate        
self.navigationController?.pushViewController(vc, animated: true)

//Inside VCB
self.delegateMethod() //Now you can call any delegate protocol methods from VCA

是的,
Delegate
是您获得所需内容的方式。在swift中完全实现委托存在一些问题。在这里,我提供了一个链接,它完全指导您如何在swift中实现委托

正如您所说,应用程序在调用代理时崩溃。这意味着,您的方法在
VCA
中不可用,或者代理没有
VCA
的引用。请检查这两个条件


谢谢

在您的示例中,您正在创建一个新的
VCA
实例。调用委托方法时,它会将其传递给新的
VCA
,在大多数情况下,这不是您想要的。谢谢您的评论。当我输入时,我意识到这不是最好的方式,并更新了我的答案,以显示如何将其设置为推送的
VC
。@Team Srijana,如果需要帮助,请告诉我