Understanding what is this “Delegate”?

让人想犯罪 __ 提交于 2019-12-02 07:44:27

问题


I'm learning Objective-C, and I have to do one app. What I'm trying to do, is call from my MainViewController, a SplitViewController, in code. That's already solved. Now, I have to call multiple DetailViewControllers. I've found this example:
http://kshitizghimire.com.np/uisplitviewcontroller-multipledetailviews-with-navigation-controller/

In this example, they use MultipleDetailViewsWithNavigatorAppDelegate to declare SplitViewController and MasterViewController, because the SplitViewController is loaded from the start, in the first ViewController, and using the didFinishLaunching function, of the AppDelegate. In my app, I'm loading the SplitViewController in the second ViewController, so my SplitViewController is declared and loaded in my ViewController, not in my AppDelegate. I've read, that "delegates" are like Interfaces, to declare and implement methods in different way, but here there are not using protocols or delegate methods. So, I have to do other AppDelegate for my SecondViewController, and declare in this new AppDelegate the SplitViewController and MasterViewController?... Or is this AppDelegate variable reference, just to reference to SplitViewController?


回答1:


From Cocoa Core Competencies:

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.



来源:https://stackoverflow.com/questions/12207148/understanding-what-is-this-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!