1 / 14

Praxisbeispiel Cocoa

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)

hamlet
Download Presentation

Praxisbeispiel Cocoa

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Wiederholung • MVC (Model-View-Controller) • Daten - Oberfläche - Verknüpfung

  3. 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 • Delegatekann jedes Objekt sein, welches Delegation Methoden implementiert

  4. Klassen Implementierung @implementation MyClass- (void)windowDidMove:(NSNotification*)notification { // ... }@end Methode: windowDidMove / windowShouldClose

  5. Instanz von MyClass MyClass *myDelegate = [[MyClass alloc] init];[window setDelegate: myDelegate];Unter NSWindow: if([[self delegate] respondsToSelector:@selector(windowDidMove:)]) {    [[self delegate] windowDidMove:notification];}

  6. Beispiel für Delegation

  7. Informelles Protokoll @interface NSObject(NSWindowNotifications)- (void)windowDidMove:(NSNotification *)notification;// ... other methods here@end • Häufig verwendet für Delegates

  8. Formelles Protokoll @protocol NSWindowNotifications@optional- (void)windowDidMove:(NSNotification *)notification;// ... other methods here@end @interface MyDelegate < NSWindowNotifications >// ...@end

  9. Observer @implementation MyObject// Sendet MyNotification Nachricht wenn aufgerufen- (void)notify {  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];}// Gibt Nachricht aus wenn MyNotification eingeht- (void)handleNotification:(NSNotification*)note { NSLog(@"Got notified: %@", note);}@endMyObject *object = [[MyObject alloc] init];// MyNotification events von allen Objekten erhalten[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(handleNotification:) name:@"MyNotification" object:nil];// eine Notification erstellen[object notify];

  10. Observer • Losere Form der Interaktion • Delegate ist fest an Objekt gebunden, Observer hat eher informativen Charakter • Registriereung bei NotificationCenter

  11. Observer @implementation MyObject// Sendet MyNotification Nachricht wenn aufgerufen- (void)notify {  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];}// Gibt Nachricht aus wenn MyNotification eingeht- (void)handleNotification:(NSNotification*)note { NSLog(@"Got notified: %@", note);}@endMyObject *object = [[MyObject alloc] init];// MyNotification events von allen Objekten erhalten[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(handleNotification:) name:@"MyNotification" object:nil];// eine Notification erstellen[object notify];

  12. Observer @implementation MyObject// Sendet MyNotification Nachricht wenn aufgerufen- (void)notify {  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];}// Gibt Nachricht aus wenn MyNotification eingeht- (void)handleNotification:(NSNotification*)note {  NSLog(@"Got notified: %@", note);}@endMyObject *object = [[MyObject alloc] init];// MyNotification events von allen Objekten erhalten[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(handleNotification:) name:@"MyNotification" object:nil];// eine Notification erstellen[object notify];

  13. Observer @implementation MyObject// Sendet MyNotification Nachricht wenn aufgerufen- (void)notify {  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];}// Gibt Nachricht aus wenn MyNotification eingeht- (void)handleNotification:(NSNotification*)note { NSLog(@"Got notified: %@", note);}@endMyObject *object = [[MyObject alloc] init];// MyNotification events von allen Objekten erhalten[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(handleNotification:) name:@"MyNotification" object:nil];// eine Notification erstellen[object notify];

  14. Quellen • http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c • http://stackoverflow.com/questions/842737/cocoa-notification-example • https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

More Related