【问题标题】:Delegate implementation in between viewcontrollers in swift快速在视图控制器之间委托实现
【发布时间】:2016-01-22 13:01:18
【问题描述】:

我有 2 个视图控制器 VCAVCB。 从VCA 移动到VCB,有一些价值,工作正常

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = somevalue
self.navigationController?.pushViewController(vc, animated: true)

但是对于反向,我想在从VCB 上传某些数据后,从VCB 调用VCA 中的一个方法。然后刷新VCA 中的文本字段值。我可以在 VCA 中出现 viewwill 中的更新代码,但由于某种原因,我没有这样做,而是尝试实现委托。 我写了一些代码:

VCA:

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

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

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

}
}

VCB:

    protocol MobileVerifyDelegate{
    func delegateMethod()
}


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

提前致谢

【问题讨论】:

    标签: ios swift delegates


    【解决方案1】:

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

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

    后者是:

    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
    

    【讨论】:

      【解决方案2】:

      我不知道你的情况是什么,但最简单的解决方案是移动委托方法并委托给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
      

      【讨论】:

      • 在您的示例中,您正在创建一个 VCA 的新实例。调用委托方法时,它会将其传递给新的VCA,这在大多数情况下不是您想要的。
      • 感谢您的评论。我在打字时意识到这不是最好的方法,并更新了我的答案以显示您如何将其设置为推送的VC
      【解决方案3】:

      是的,Delegate 是你获得你想要的东西的方式。在 swift 中完全实现委托存在一些问题。在这里,我提供了一个链接,它可以完全指导您如何在 swift 中实现委托。

      Delegate In Swift

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

      谢谢

      【讨论】:

      • @Team Srijana,如果您需要任何帮助,请告诉我
      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多