2016-11-17 23 views
0

我有兩個視圖控制器A & B.從我發回一些數據到視圖控制器。我B ViewController嵌入在NavigationController。現在我要從下面的代碼從A到B.在這種情況下,我的委託方法不會被調用。設置viewcontoller的代表問題?

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) 

請提出一個更好的方法。

+0

你是什麼意思,你的委託方法永遠不會被調用?你在哪裏試圖調用委託方法? – AdamPro13

+0

在第一個視圖控制器中 – Techiee

回答

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) 
} 
0

Nirav D是正確的。

其實你的代碼的問題是,它正在創建兩個視圖控制器'DropboxListingViewController'的實例。您將當前視圖控制器設置爲委託的實例實際上從未加載到內存中,導航控制器創建了自己的「DropboxListingViewController」實例,但您尚未自行註冊該實例。 @deepak