【问题标题】:issue with Setting delegate of viewcontoller?视图控制器的设置委托问题?
【发布时间】:2016-11-17 05:35:58
【问题描述】:

我有两个视图控制器 A 和 B。从我将一些数据发送回 A 视图控制器。我的 B ViewController 嵌入在 NavigationController 中。现在我要从下面的代码从 A 到 B。在这种情况下,我的委托方法不会被调用。

let storyboard = UIStoryboard(name: "DropboxView", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "nav_drop") as! UINavigationController
let vc1:DropboxListingViewController = storyboard.instantiateViewController(withIdentifier: "DropboxListingViewController") as! DropboxListingViewController
vc1.CTprotocolDelegate = self
self.present(vc, animated: true, completion: nil) 

请提出更好的方法。

【问题讨论】:

  • 你的委托方法永远不会被调用是什么意思?您在哪里尝试调用委托方法?
  • 在第一个视图控制器中

标签: ios iphone swift uinavigationcontroller swift3


【解决方案1】:

您需要从您的UINavigationController 对象访问DropboxListingViewController,在您的情况下它是vc,就像这样。

let storyboard = UIStoryboard(name: "DropboxView", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "nav_drop") as! UINavigationController
if let vc1 = vc.viewControllers.first as? DropboxListingViewController {
    vc1.CTprotocolDelegate = self
    self.present(vc, animated: true, completion: nil) 
}

【讨论】:

    【解决方案2】:

    Nirav D 是正确的。

    实际上,您的代码的问题在于它正在创建视图控制器“DropboxListingViewController”的两个实例。您将当前视图控制器设置为委托的实例实际上从未加载到内存中,而是导航控制器创建了自己的“DropboxListingViewController”实例,您尚未注册自己。 @deepak

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 2015-02-26
      相关资源
      最近更新 更多