18

In some of my learning I saw someone mention that in your view controller you can have a model and have some sort of listener on the model for changes in it. I don't think I'm using the right names for these, which is probably why my searches haven't turned up anything. Essentially I want to move my server calls from the controllers into my models, but I need some sort of listener on them to know when the call is complete to update my views.

1

5 Answers 5

20

look into delegates delegates tutorial

or blocks a bit more advanced basic blocks tutorial

just start with delegates,

you can also use NSNotification NSNotification tutorial but is not recommended as it broadcast to every class, and you might only need to send messages to a specific class not every one

4
  • 1
    I believe what I read was about the NSNotification, the reason I was leaning towards that is because I want the Model to be unaware of or uncaring about what was listening. When I've implemented delegates in other places I needed to know about the delegate so I could call a specific method on it. In this case I want to model to just broadcast that it finished, so whichever view might be listening for it can act in whatever way is appropriate for that specific view.
    – Jhorra
    Commented May 8, 2012 at 3:36
  • first link broken Commented Jun 15, 2017 at 9:19
  • Broken link, should be tutorialspoint.com/ios/ios_delegates.htm (trailing "/" should be removed)
    – Cris
    Commented Dec 26, 2017 at 14:36
  • @manuelBetancurt seems the "basic blocks tutorial" is not available anymore. The website seems lost. Commented May 19, 2019 at 12:57
13

Belong to C# world, i have to go to objective c (for my job). I think the event equivalent in objective c is this implementation :

Create protocol with all your event's methods :

@protocol MyDelegate <NSObject>
    - (void)myEvent;
@end

In your class which should send the event, add :

@interface MyClassWichSendEvent : NSObject

@property (nonatomic, retain) IBOutlet id<MyDelegate> delegate;

@end

Raising the event where you want, for example :

- (IBAction)testEvent:(NSButton*)sender
{
    [self.delegate myEvent];
}

Now in your listener class, you should listen the events like this :

Add the protocol to your class that listening

@interface Document : NSDocument<MyDelegate>

In the implementation, on init or in interface builder, you must link delegate of your object instance to listen with self of your class which listen.

In code

-(void)awakeFromNib
{
    myObjToListen.delegate = self;    
}
  • In Interface Builder -> IBOutlet from delegate to your listen's class.

And finally, implement your method in your listener class :

- (void)myEvent
{
    NSLog(@"i have listen this event !");
}

Sorry for my english, i hope that help people who went from java or C#.

1
  • What I don't understand how do you call myEvent on an id? Because what I see is that delegate is an IBOutlet id<MyDelegate>. Can you please explain a bit in details? Thank you Commented Jun 15, 2017 at 9:20
8

You're looking for KVO - key/value observing:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

http://nachbaur.com/2011/07/29/back-to-basics-using-kvo/

Delegates + Notifications are good for communicating between objects but they don't automatically send msgs when a value changes (which from your question, that is what you are asking about)

0

I think you may be looking for NSNotificationCenter which is a way to pass messages to whoever may be listening. So you can send out a notification from an event in your model and just listen for it in your controllers. A cleaner method might be to implement your own protocol with delegates.

0

Objective C uses delegates

This post has a nice example: How do I create delegates in Objective-C?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.