-2

i need explanation of how could NSObject Class be a delegate to any controller although NSObject is not a controller !?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    A delegate doesn't have to be a controller. All it needs to do is provide the required methods defined by the delegate protocol. – Phillip Mills Jan 13 '19 at 14:28
  • Could be related: https://stackoverflow.com/questions/41271202/why-do-we-need-to-set-delegate-to-self-why-isnt-it-defaulted-by-the-compiler/41271593#41271593 ("If there is a chance that tableView.delegate is set to something other than self...well what is that? Can you please provide some examples?") – Ahmad F Jan 13 '19 at 14:31
  • I am sorry I think this is a reasonable question. It doesn't need clarification it just needs more effort shown as to what the OP has done to find the answer, – user3069232 Dec 18 '20 at 11:18

4 Answers4

3

A delegate is just an implementation of Delegate design pattern. In Cocoa classes it is implemented using Protocols - any class that implements a specific protocol can be a delegate. For example, let's look at the definition of delegate property in UITableView class (in Objective-C, because it's better at showing distinction between classes and protocols):

@property(nonatomic, weak) id<UITableViewDelegate> delegate;

In Objective-C, id is a universal pointer - a pointer to object of any class. You can see that UITableView expects it's delegate to be of any class, but a class that implements UITableViewDelegate protocol

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
0

An object can be a delegate without being a controller.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • can you explain please ? how could that ? for example UITableViewController can be a delegate for UITableViewCells ? i know it happened but i don't understand the reason or conditions of being a delegate – slash gamer Jan 13 '19 at 14:26
  • A delegate in Objective-C is like a listener in Java. A delegate can be any object implementing a protocol, or interface if you want to use a Java terminology. Any object can be a delegate or a listener for any other object, and the contract that links the 2 objects, one considered as a source, and the orther (the delegate) as a receiver, is the protocol. So, the delegation is a way of notifying one object (the receiver) of an event happening on the source object. The source object doesn't need to know of which type is the delegate (the listener) but only the protocol it implements. – Hichem BOUSSETTA Jan 13 '19 at 14:40
  • you can find a concrete example of creating a delegate in this topic: https://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – Hichem BOUSSETTA Jan 13 '19 at 14:42
0

Being a delegate just means that you agree to respond to a set of methods. It doesnt matter what kind of object you are, as long as you understand the methods in the delegate protocol, you can be a delegate.

Say, for example, you have a central table view manager object that knows about mulitple table views throughout your UI. You could make that table view manager the delegate of all the table views in your app. (I don’t know if this would make sense, but you could certainly do it if you had a reason to do so.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

The only requirement for a class to be a delegate for a particular class is to implement the protocol required. It doesn't depend upon its inheritance chain. It could be uiviewcontroller, uitableviewcontroller or simply the NSObject.

If your class confirms to the required protocol then its a fair candidate to be the delegate.

HAK
  • 2,023
  • 19
  • 26