Praxisbeispiel Cocoa Universität zu Köln Historisch-Kulturwissenschaftliche Informationsverarbeitung Re-usable Content in 3D und Simulationssystemen Prof. Dr. Manfred Thaller, SS 2012 Referent: Jan Moritz Kohl, 3. April 2012
Wiederholung MVC (Model-View-Controller) Daten - Oberfläche - Verknüpfung
Wiederholung Hauptobjekt = NSApplication Keine Ableitung zur Erstellung individueller Klassen sondern Delegation (Zusätzliche Klassen) bzw. Erweiterung durch Kategorien Bei Eingabe des Benutzers schickt NSApplication Nachricht an Delegate Delegate kann jedes Objekt sein, welches Delegation Methoden implementiert
Klassen MyClass - (void)windowDidMove:(NSNotification*)notification { //... Methode: windowDidMove / windowShouldClose
Instanz von MyClass MyClass *myDelegate = [[MyClass alloc] init]; [window setDelegate: myDelegate]; Unter NSWindow: if([[self delegate] { [[self delegate] windowDidMove:notification]; }
Beispiel für Delegation
Informelles NSObject(NSWindowNotifications) - (void)windowDidMove:(NSNotification *)notification; //... other methods Häufig verwendet für Delegates
Formelles - (void)windowDidMove:(NSNotification *)notification; //... other MyDelegate
MyObject // Sendet MyNotification Nachricht wenn aufgerufen - (void)notify { [[NSNotificationCenter defaultCenter] object:self]; } // Gibt Nachricht aus wenn MyNotification eingeht - (void)handleNotification:(NSNotification*)note { notified: note); MyObject *object = [[MyObject alloc] init]; // MyNotification events von allen Objekten erhalten [[NSNotificationCenter defaultCenter] addObserver:object object:nil]; // eine Notification erstellen [object notify];
Observer Losere Form der Interaktion Delegate ist fest an Objekt gebunden, Observer hat eher informativen Charakter Registriereung bei NotificationCenter
MyObject // Sendet MyNotification Nachricht wenn aufgerufen - (void)notify { [[NSNotificationCenter defaultCenter] object:self]; } // Gibt Nachricht aus wenn MyNotification eingeht - (void)handleNotification:(NSNotification*)note { notified: note); MyObject *object = [[MyObject alloc] init]; // MyNotification events von allen Objekten erhalten [[NSNotificationCenter defaultCenter] addObserver:object object:nil]; // eine Notification erstellen [object notify];
MyObject // Sendet MyNotification Nachricht wenn aufgerufen - (void)notify { [[NSNotificationCenter defaultCenter] object:self]; } // Gibt Nachricht aus wenn MyNotification eingeht - (void)handleNotification:(NSNotification*)note { notified: note); MyObject *object = [[MyObject alloc] init]; // MyNotification events von allen Objekten erhalten [[NSNotificationCenter defaultCenter] addObserver:object object:nil]; // eine Notification erstellen [object notify];
MyObject // Sendet MyNotification Nachricht wenn aufgerufen - (void)notify { [[NSNotificationCenter defaultCenter] object:self]; } // Gibt Nachricht aus wenn MyNotification eingeht - (void)handleNotification:(NSNotification*)note { notified: note); MyObject *object = [[MyObject alloc] init]; // MyNotification events von allen Objekten erhalten [[NSNotificationCenter defaultCenter] addObserver:object object:nil]; // eine Notification erstellen [object notify];
Quellen delegates-in-objective-c delegates-in-objective-c notification-example notification-example coa/Conceptual/CocoaFundamentals/CommunicatingWithObj ects/CommunicateWithObjects.html coa/Conceptual/CocoaFundamentals/CommunicatingWithObj ects/CommunicateWithObjects.html