CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController...

37
CS193E Lecture 5 Loading Resources Notifications System Panels 1 Wednesday, January 23, 2008

Transcript of CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController...

Page 1: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

CS193ELecture 5

Loading ResourcesNotificationsSystem Panels

1Wednesday, January 23, 2008

Page 2: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Today’s Topics• Questions on the Favorite Things 1 assignment?• Loading resources

■ Additional nib files■ Arbitrary resources

• Notifications• System Panels

2Wednesday, January 23, 2008

Page 3: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Build Errors/Warnings• The Debug configuration can sometimes mask build warnings/

errors with ZeroLink feature■ Build Clean

Build > Clean■ Use release configuration instead of debug

Project > Set Active Build Configuration > Release■ Build with new configuration

Build > Build

3Wednesday, January 23, 2008

Page 4: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Demo

Favorite Things II

4Wednesday, January 23, 2008

Page 5: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Resources and Multiple Nib Files

5Wednesday, January 23, 2008

Page 6: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Applications use multiple components

6Wednesday, January 23, 2008

Page 7: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Implement using multiple nib files• Components—modular design and implementation• Efficiency—lazy instantiations avoid unnecessary file I/O,

resulting in decreased startup times• Reuse and Replication• Dynamic replacement

7Wednesday, January 23, 2008

Page 8: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSApplication loads and owns main nib

8Wednesday, January 23, 2008

Page 9: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

File’s Owner lives outside the nibOften is the object that loads the nib

NSWindowController

9Wednesday, January 23, 2008

Page 10: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSWindowController• A window controller

■ Manages a single window■ Loads the window's nib file■ Frees other top-level objects within the nib

• You typically subclass NSWindowController

10Wednesday, January 23, 2008

Page 11: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSWindowController methodsFrequently used

- (IBAction) showWindow: (id) sender

- (NSWindow *) window

Commonly overridden

- (NSString *) windowNibName

- (void) windowWillLoad

- (void) windowDidLoad

- (NSString *) windowTitleForDocumentDisplayName: (NSString *) displayName

11Wednesday, January 23, 2008

Page 12: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Demo

Adding a second nib fileLoading a second windowSetting bundle identifier and icon

12Wednesday, January 23, 2008

Page 13: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Dynamically-loaded resources• Nib files• Images and sounds• Localized character strings• Executable code and class implementations

13Wednesday, January 23, 2008

Page 14: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSBundle interface+ (NSBundle *)mainBundle;

+ (NSBundle *)bundleWithPath:(NSString *) path;

+ (BOOL)loadNibNamed:(NSString *) name owner:(id)owner;

- (NSString *)pathForResource:(NSString *) name ofType:(NSString *)extension;

- (NSString *)localizedStringForKey:(NSString *) keyvalue:(NSString *)value table:(NSString *)tableName;

- (Class)classNamed:(NSString *)className;

14Wednesday, January 23, 2008

Page 15: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Notifications

15Wednesday, January 23, 2008

Page 16: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Notifications• Facilitate loose coupling• General purpose 1-to-N communication• Lets you “broadcast” messages• Notifications have a name, an object and an optional “userInfo”

dictionary• Coordinated through a communication “center”

16Wednesday, January 23, 2008

Page 17: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Example notification names• NSApplicationWillFinishLaunchingNotification• NSApplicationDidFinishLaunchingNotification• NSSplitViewDidResizeSubviewsNotification• NSTextDidChangeNotification• NSWindowDidResizeNotification

17Wednesday, January 23, 2008

Page 18: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Notifications

Notification Center

“Poster”

“Observers”

Window “posts” a notification

Notification Centerbroadcasts to observers

Window wants to announcewindowDidBecomeMain:

18Wednesday, January 23, 2008

Page 19: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Registering for a notification

// Get the notification center NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

// register as an observer [center addObserver:self selector:@selector(windowBecameMain:) name:NSWindowDidBecomeMainNotification object:theWindow];

19Wednesday, January 23, 2008

Page 20: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Observing Options• Specific notification from a specific object

[center addObserver:self selector:@selector(objectDidSomething:) name:@”DoSomething” object:someObject];

• All notifications from a specific object[center addObserver:self selector:@selector(objectDidSomething:) name:nil object:someObject];

• Specific notification from any object[center addObserver:self selector:@selector(windowBecameMain:) name:@“WindowDidBecomeMain” object:nil];

20Wednesday, January 23, 2008

Page 21: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Callback Conventions• Notification callback methods return void and take a single

argument: the notification• - (void)windowBecameMain:(NSNotification *)notification

{ NSString *name = [notification name]; id object = [notification object]; NSDictionary *userInfo = [notification userInfo];

// handle the window activation...}

21Wednesday, January 23, 2008

Page 22: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Removing Observers

• Notification centers don’t retain observers• Before an object is dealloced, it must be cleared out of

the notification center! - (void)removeObserver:(id)observer;

• For example, - (void)dealloc { [center removeObserver:self ]; [super dealloc];

}

22Wednesday, January 23, 2008

Page 23: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Removing Options• Specific notification from a specific object

[center removeObserver:self name:@”DoSomething” object:someObject];

• All notifications from a specific object[center removeObserver:self name:nil object:someObject];

• Specific notification from any object[center removeObserver:self name:@“WindowDidBecomeMain” object:nil];

• Any notification from any object[center removeObserver:self ];

23Wednesday, January 23, 2008

Page 24: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Posting a notification

// Get the notification center NSNotificationCenter *center = [NSNotificationCenter defaultCenter];// Post the notification [center postNotificationName: @“MyCustomNotification” object:theWindow];

24Wednesday, January 23, 2008

Page 25: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Notification Miscellany• Notification names are just strings• Adding custom notifications is trivial• Some delegate callbacks actually use notifications; delegate

automatically registered when set as the delegate

25Wednesday, January 23, 2008

Page 26: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Notification versus delegation• To-many relationship• Not used to alter behavior of the posting object

• Note: Some Application Framework classes automatically register delegates to receive selected notifications.

26Wednesday, January 23, 2008

Page 27: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Panels, Sheets, and Alerts

27Wednesday, January 23, 2008

Page 28: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Panels, Sheets, Alerts• Panels

■ An NSPanel is a subclass of NSWindow■ Used for auxiliary windows such as inspectors■ Some different default behaviors than windows

• Alerts■ An NSAlert is a configurable object to present a warning or alert

to the user as a modal window or as a sheet

• Sheets■ A sheet is a window or panel run in a modal fashion in

conjunction with another window■ There is no ‘NSSheet’ class

28Wednesday, January 23, 2008

Page 29: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Standard AppKit panels• Provide a consistent look and feel• New features picked up automatically by application

29Wednesday, January 23, 2008

Page 30: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSFontPanel and NSColorPanel

30Wednesday, January 23, 2008

Page 31: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSSavePanelUsed as window or sheet

31Wednesday, January 23, 2008

Page 32: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

NSOpenPanelUsed as window or sheet

32Wednesday, January 23, 2008

Page 33: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Using NSOpenPanel- (IBAction)chooseFile:(id)sender {

NSOpenPanel *openPanel = [NSOpenPanel openPanel];// configure open panel[openPanel setAllowsMultipleSelection:NO];[openPanel setTitle:@”Select file to import”];// nil for types allows any typeint result = [openPanel runModalForTypes:nil];if (result == NSOKButton) {

NSString *filename = [openPanel fileName];// do something with filename

}}

33Wednesday, January 23, 2008

Page 34: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Sheets provide window-level modality

34Wednesday, January 23, 2008

Page 35: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Creating an alert sheet- (BOOL) windowShouldClose:(id)sender { NSAlert *alert = [[[NSAlert alloc] init] autorelease];

[alert addButtonWithTitle:@"Save"];[alert addButtonWithTitle:@"Cancel"];[alert addButtonWithTitle:@"Don’t Save"];[alert setMessageText:@"Unsaved Changes"];[alert setInformativeText:@"You’ll lose unsaved changes."];[alert setAlertStyle:NSWarningAlertStyle];

[alert beginSheetModalForWindow:windowmodalDelegate: selfdidEndSelector: @selector(alertDidEnd:returnCode:contextInfo:)contextInfo: nil];

return NO;}

35Wednesday, January 23, 2008

Page 36: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Alert sheet callback method- (void) alertDidEnd:(NSAlert *)alert returnCode:(int) code contextInfo:(void *)context {

switch(code) {

case NSAlertFirstButtonReturn:[window close]; // note -close NOT -close:break;

default:break;

}

}

36Wednesday, January 23, 2008

Page 37: CS193E Lecture 5 - web.stanford.edu · Often is the object that loads the nib NSWindowController Wednesday, January 23, 2008 9. NSWindowController

Questions?

37Wednesday, January 23, 2008