Is it necessary for a delegate message to always be empty as a return type?

I have a scenario in which Obj A sends a message to the delegate of Obj B at the click of a button. Obj B takes some action based on a delegate call.

Obj A wants to show something on the screen based on the result of what happened after the delegate message was called.

Say that Obj B maintains a counter, how many times this action has taken place.

So, when Obj A presses the buttons and calls the delegate method, is it a good idea for this delegate method to return a value, in my case, the current counter?

so that Obj A can display the updated counter value.

In this case, the same message acts as a delegate and data source.

For me, Obj A is View and Obj B. This is a view controller.

Is my implementation erroneous?

+6
source share
3 answers

I like your implementation.

Even if he does not distinguish between the delegate and the roles of the data source, he combines them in a more simplified way.

I assume that a suitable way to implement regarding MVC concepts is:

  • A (view) sends a message to B (delegate) with the message "I was listening" or something similar.

  • B (delegate) acts as needed and sends back A message with the message "reloadData".

  • A requests B (as the data source now) to display the data.

since in your case the delegate and the data source are the same object, as it happens in many other cases, it seems to me very plausible and not at all wrong to implement the stream, as you did:

  • A (view) sends a B (delegate and data source) message and receives back the data information needed to update the view.
+5
source

no, and in many cases it does not return void, but a value.

This is a valid and frequently used approach. One example is a UITextView delegate:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

but MANY other components do this, so they donโ€™t need to be too specialized, but so that they can remain universal

+3
source

No, itโ€™s not a rule that a delegate should return void.

There are many delegates that return non-void values.

 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename; // NSApplication - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url; // UIApplicationDelegate - (UITableRowIndexSet *)tableView:(NSTableView *)tableView willSelectRows:(UITableRowIndexSet *)selection; // UITableViewDelegate - (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame; // NSWindow 
+2
source

All Articles