How to require a tableview's datasource to conform to my custom protocol

I have subclassed NSTableView and NSViewController. The viewcontroller is set as the datasource of the tableview. I have declared a protocol in my tableview. Making the viewcontroller conform to my protocol is easy. The problem is making the tableview aware that its datasource conforms to my protocol so I can call its methods.


Thanks

In your subclass, redeclare the delegate property as conforming to your protocol. I assume your protocol is an extension of NSTableViewDelegate. So, you'd declare the property as:

- (void)setDelegate:(id <YourProtocol>)delegate;
- (id <YourProtocol>)delegate;

or, possibly:

@property (assign) id<YourProtocol> delegate;


In your implementation, you could implement overrides of the accessors which just call through to super. The getter will have to do a type cast since super's declaration is in terms of NSTableViewDelegate.


Or, if you used the property declaration, you can simply do:

@dynamic delegate;

which tells the compiler that the accessors will be available through some means that isn't explicit in this implementation (i.e. inherited).


Now, when you invoke self.delegate, it will have the desired type.

Here is one solution for your question. If it's helpful please click one of the links below to let me know, otherwise feel free to refine your question a bit and I will update if needed. It's not different from the other answer but it explains the overview of how you use protocols to communicate from the model (or view) to the controller and back again. Remember to not communicate between two (model/view) subclasses but instead always have the view controller manage any interactions or exchange of data. If you consider all of these points, your controller will communicate to your subclasses using the protocol functions and the reference to the subclass itself (via the controller property of type subclass; note here that you can add other useful public properties to the subclass that the controller might find interesting)


  1. The typical pattern that you'll want to follow is to simply add a property to your view / model subclass (in this case your NSTableView custom subclass).
  2. Make that property of type ProtocolName (the property is how your view or model subclass will communicate to any controller)
  3. Then within the subclass, at an appropriate point, you can reference the property (of the protocol type) to call your protocol functions on the view controller (use code completion on the property to see this in action).
  4. On your controller create an implementation for all the protocol behaviors. (this should do whatever you want when the subclass calls it)
  5. On your controller create a reference to your subclass as a property of the subclass type
  6. On your controller remember to set the delegate for the reference created in step 5 to self (put this somewhere useful in the lifecycle like viewdidLoad)
  7. On your controller remember to add your custom protocol to the list of classes and protocols that the UIViewController supports (and verify step - 4)
How to require a tableview's datasource to conform to my custom protocol
 
 
Q