cs193e lecture 5 - stanford universityweb.stanford.edu/class/cs193e/downloads/05-resources... ·...
TRANSCRIPT
![Page 1: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/1.jpg)
CS193ELecture 5
Loading ResourcesNotificationsSystem Panels
1Wednesday, January 23, 2008
![Page 2: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/2.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/3.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/4.jpg)
Demo
Favorite Things II
4Wednesday, January 23, 2008
![Page 5: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/5.jpg)
Resources and Multiple Nib Files
5Wednesday, January 23, 2008
![Page 6: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/6.jpg)
Applications use multiple components
6Wednesday, January 23, 2008
![Page 7: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/7.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/8.jpg)
NSApplication loads and owns main nib
8Wednesday, January 23, 2008
![Page 9: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/9.jpg)
File’s Owner lives outside the nibOften is the object that loads the nib
NSWindowController
9Wednesday, January 23, 2008
![Page 10: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/10.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/11.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/12.jpg)
Demo
Adding a second nib fileLoading a second windowSetting bundle identifier and icon
12Wednesday, January 23, 2008
![Page 13: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/13.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/14.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/15.jpg)
Notifications
15Wednesday, January 23, 2008
![Page 16: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/16.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/17.jpg)
Example notification names• NSApplicationWillFinishLaunchingNotification• NSApplicationDidFinishLaunchingNotification• NSSplitViewDidResizeSubviewsNotification• NSTextDidChangeNotification• NSWindowDidResizeNotification
17Wednesday, January 23, 2008
![Page 18: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/18.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/19.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/20.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/21.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/22.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/23.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/24.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/25.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/26.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/27.jpg)
Panels, Sheets, and Alerts
27Wednesday, January 23, 2008
![Page 28: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/28.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/29.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/30.jpg)
NSFontPanel and NSColorPanel
30Wednesday, January 23, 2008
![Page 31: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/31.jpg)
NSSavePanelUsed as window or sheet
31Wednesday, January 23, 2008
![Page 32: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/32.jpg)
NSOpenPanelUsed as window or sheet
32Wednesday, January 23, 2008
![Page 33: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/33.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/34.jpg)
Sheets provide window-level modality
34Wednesday, January 23, 2008
![Page 35: CS193E Lecture 5 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/35.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/36.jpg)
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 - Stanford Universityweb.stanford.edu/class/cs193e/Downloads/05-Resources... · 2008. 1. 25. · NSWindowController •A window controller Manages a single window](https://reader035.vdocuments.site/reader035/viewer/2022071009/5fc76ea535059622f15853c9/html5/thumbnails/37.jpg)
Questions?
37Wednesday, January 23, 2008