Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple delegates in objective-c

i have been working on moving one of my apps away from the "shared appdelegate" process which seems to be frowned up, despite its over whelming use. i have been attempting to setup protocol methods for what i want to do but am having zero luck. my question is, can you even have lets say a single viewcontroller send delegate requests to multiple classes? from what im finding out it doesn't seem like you can. which doesn't make sense because i thought that was the whole point of delegates and protocols with mvc. now just to clarify, i know you can have a single viewcontroller act as the delegate for multiple other viewcontrollers. but that's not what i am asking. for a simple example, lets say you have apples flip-utility template. the "done" button just calls a delegate method to the mainvc to dismiss it. now lets say we added a new class called...

@interface NewClass : NSObject <TheOtherDelegate>

and it had a delegate method...

- (void)doSomething
{
NSLog(@"The Delegate did something...");
}

can we have a button on the flipsideviewcontroller, that we wanted to call that delegate method, but still keep the "done" button call to the delegate method on the mainviewcontroller that dismisses it?

that being said, i put together a quicky project just to see if it would work and it doesn't. i came across an "answer" that says you have to instantiate the class first you want to be the delegate...

NewClass *myDelegate = [NewClass alloc] init]
[fillInMethodHere setDelegate:myDelegate];

not sure why it got a correct answer check, because needless to say it doesn't work. is there something i am missing? i scoured ib to see if there is some "delegate" connection somewhere but i couldn't find anything.

on a side note, as i was working in my working project, i read a suggestion about removing the #import and adding @class. again, that broke all kinds of things. the strange thing is before doing that, what i had so far was working and building fine. when i removed the new @class and un-commented the #import. xcode all of a sudden gave me an error "cannot find protocol deceleration for..." but yet, it worked seconds earlier. i would up having to remove the protocol code and re-add it for it to work again. very starge.

any help would be appreciated. everything iv read in docs, google, stack, etc that say something should work, don't in an actual project.

like image 450
DoS Avatar asked Dec 17 '22 00:12

DoS


1 Answers

A "delegate" isn't some fancy object. It's simply a synthesized property of type id called delegate. If you wanted to, you could have an arbitrary number of properties that all conformed to the same protocol. Then when you wanted to issue a callback, you would just address all of them:

[self.mydelegateA doSomething];
[self.mydelegateB doSomething];

etc.

You could also have an NSMutableArray property that you could add objects to, and then use [self.myMutableArrayOfDelegates makeObjectsPerformSelector:@selector(doSomething)].

Finally, there's always the route of NSNotificationCenter (not to be confused with push notifications) is a class that provides an inter-object messaging system. Many objects can register for a message that any other object can send.

Please see the Apple's documentation for more information. Click Here.

Regardless of the fact that this is OS X documentation, it's still quite good at explaining things visually: click here.

Here's an example of simply changing the name of the delegate property: click here

And here's an example of adding another protocol and a second delegate: click here

Finally, here's an example that builds on the previous two and has a third delegate that also conforms to the same protocol: click here

like image 175
Jack Lawrence Avatar answered Dec 28 '22 08:12

Jack Lawrence