2016-01-23 86 views
-1

首先,我不得不說我對於快捷和客觀的C是非常新的。我正在自己學習。爲什麼要將viewController作爲變量傳遞?

我有一個問題,這個代碼

我有我的SettingViewController稱爲「settingsViewControllerFinished」,它通過對整個控爲變量的代表。 了這樣的代碼:

我SettingViewController.swift

protocol SettingViewControllerDelegate: class { 
func settingsViewControllerFinished(settingsViewController: SettingsViewController) 

}

@IBAction func close(sender: AnyObject) { 
dismissViewControllerAnimated(true, completion: nil) 
self.delegate?.settingsViewControllerFinished(self) 

}

我confused.What你的意思是,如果你通過整個控制器一個變量?(也許這個問題很愚蠢)


在我的viewController:

func settingsViewControllerFinished(settingsViewController: SettingsViewController) 
{ 
    self.brushWidth = settingsViewController.brush 
    self.opacity = settingsViewController.opacity 
    self.red = settingsViewController.red 
    self.green = settingsViewController.green 
    self.blue = settingsViewController.blue 
} 

我猜的理由是:我通過一切SettingViewController到視圖控制器,這樣我可以在SettingViewController使用的變量。

我是否是rihgt?

回答

1

一般來說,你是正確的,:傳遞SettingViewController回其委託使原來調用者不必保持一個參考的創建,並自委託方法一起已經將相關信息發送SettingViewController所示。

但還有更多:在某些代表的情況下,這種風格對於不同的東西是有用的。設想一個由功能func somethingGotClicked(sender:YourSenderType)組成的點擊處理程序。如果您的班級創建YourSenderType的多個實例並將它們同時顯示爲自己的代表,則無法知道如果沒有sender參數,則會點擊哪個。在一些func somethingGotClicked()你不知道哪個被點擊。當顯示多個UITableViewUICollectionView時,通常需要該功能是一個單一視圖,其中一個實例設置爲其代表。

+0

謝謝你我的朋友! – iceChao

相關問題