Cocoa GUI
Foundation AppKit
GUI
Foundation AppKit
5.1 Run Loop
Cocoa run loop Run
loop
Cocoa
5.3
run loop NSDistributeNotifi cationCenter
Objective-C
116 5
Cocoa run loop
Cocoa Cocoa
run loop
NSTimer
AppKit
run loop UNIX talk
� LineBuffer NSFileHandle
� Sender
� Receiver
Run loop
LineBuffer 5.1
101~104 NSNotifi cationCenter nil
105
-readData:
110~114
1175.1 Run Loop
5.1 examples/Notifi cations/distributedNotify.m
88 @interface LineBuffer : NSObject {89 NSMutableString *buffer;90 }91 - (id) initWithFile: (NSFileHandle*)aFileHandle;92 @end93 @implementation LineBuffer94 - (id) initWithFile: (NSFileHandle*)aFileHandle95 {96 if (nil == (self = [self init]))97 {98 return nil;99 }
100 buffer = [[NSMutableString alloc] init];101 NSNotificationCenter *center =102 [NSNotificationCenter defaultCenter];103 [center addObserver: self104 selector: @selector(readData:)105 name: NSFileHandleDataAvailableNotification106 object: aFileHandle];107 [aFileHandle waitForDataInBackgroundAndNotify];108 return self;109 }110 - (void) readData: (NSNotification*)aNotification111 {112 NSFileHandle *handle = [aNotification object];113 NSData *data = [handle availableData];114 NSString *str =115 [[NSString alloc] initWithData: data116 encoding: NSUTF8StringEncoding];117 [buffer appendString: str];118 [str release];119 NSArray *lines =120 [buffer componentsSeparatedByString: @"\n"];121 NSNotificationCenter *center =122 [NSNotificationCenter defaultCenter];123 // The last object in the array will be the newline string124 // if a new line is found, or the unterminated line125 for (unsigned int i=0 ; i
118 5
139 NSNotificationCenter *center =140 [NSNotificationCenter defaultCenter];141 [center removeObserver: self];142 [buffer release];143 [super dealloc];144 }
NSString componentsSeparatedByString:
@""
1
Sender 5.2 57~60
5.2 examples/Notifi cations/distributed
Notify.m
42 @end4344 @interface Sender : NSObject {45 NSString *name;46 }47 - (id) initWithName: (NSString*)aName;48 @end49 @implementation Sender50 - (id) initWithName: (NSString*)aName51 {52 if (nil == (self = [self init]))53 {54 return nil;55 }56 name = [aName retain];57 NSNotificationCenter *center =58 [NSNotificationCenter defaultCenter];59 [center addObserver: self60 selector: @selector(sendMessage:)61 name: LineInputNotification62 object: nil];63 return self;
1 NSFileHandle
1195.1 Run Loop
64 }65 - (void) sendMessage: (NSNotification*)aNotificaton66 {67 NSNotificationCenter *center =68 [NSDistributedNotificationCenter defaultCenter];69 NSString *line =70 [[aNotificaton userInfo] objectForKey: @"Line"];71 NSDictionary *message =72 [NSDictionary dictionaryWithObject: line73 forKey: @"message"];74 [center postNotificationName: ChatMessageNotification75 object: name76 userInfo: message];77 }78 - (void) dealloc79 {80 NSNotificationCenter *center =81 [NSNotificationCenter defaultCenter];82 [center removeObserver: self];83 [name release];84 [super dealloc];85 }86 @end
75
Receiver 5.3
C
printf() Objective-C C C++
C Objective-C
C C
5.4
main()
NSUserDefaults
-name
"anon"
120 5
5.3 examples/Notifi cations/distributed Notify.m
9 @interface Receiver : NSObject {}10 - (void) receiveNotification: (NSNotification*)aNotification;11 @end1213 @implementation Receiver14 - (id) init15 {16 if (nil == (self = [super init]))17 {18 return nil;19 }20 // register to receive notifications21 NSNotificationCenter *center =22 [NSDistributedNotificationCenter defaultCenter];23 [center addObserver: self24 selector: @selector(receiveNotification:)25 name: ChatMessageNotification26 object: nil];27 return self;28 }29 - (void) receiveNotification: (NSNotification*)aNotification30 {31 printf("%s: %s\n",32 [[aNotification object] UTF8String],33 [[[aNotification userInfo] objectForKey: @"message"] UTF8String]);34 }35 - (void) dealloc36 {37 NSNotificationCenter *center =38 [NSDistributedNotificationCenter defaultCenter];39 [center removeObserver: self];40 [super dealloc];
5.4 main() examples/Notifi cations/distributed Notify.m
147 int main(void)148 {149 [NSAutoreleasePool new];150 // Set up the receiver151 Receiver *receiver = [Receiver new];152 NSString *name =153 [[NSUserDefaults standardUserDefaults]154 stringForKey: @"name"];155 if (nil == name)156 {157 name = @"anon";158 }159 [[Sender alloc] initWithName: name];160 [[LineBuffer alloc] initWithFile:
1215.1 Run Loop
161 [NSFileHandle fileHandleWithStandardInput]];162 [[NSRunLoop currentRunLoop] run];163 return 0;164 }
162
-run
149
NSRunLoop run-loop
run loop autorelease
run
loop
$ gcc -framework Foundation -std=c99 distributedNotify.m$ ./a.out -name DavidThomas: HelloHiDavid: HiHow are you?David: How are you?Thomas: Fine thanks.
$ ./a.out -name ThomasHelloThomas: HelloDavid: HiDavid: How are you?Fine thanks.Thomas: Fine thanks.
Run loop
122 5
5.2
C main() Cocoa
Objective-C
Cocoa Xcode
return NSApplicationMain(argc, argv);
OpenStep
void NSApplicationMain(int argc, char *argv[]){
[NSApplication sharedApplication];[NSBundle loadNibNamed: @"myMain" owner: NSApp];[NSApp run];
}
nib
bundle
NSApplication
NSApp
Nib owner nib
nib nib
-run
NSRunLoop
NSApp
responder chain
NSApplication -setDelegate:
nib
- (void)applicationWillFinishLaunching: (NSNotification*)aNotification;- (void)applicationDidFinishLaunching: (NSNotification*)aNotification;
1235.3
Cocoa should will did
should will did
should
-applicationWillFinishLaunching: run loop
-applicationDidFinishLaunching:
-run NSApp
licationDidFinishLaunchingNotifi cation
-applicationDidHide:
Foundation AppKit AppKit
NSApplication
Cocoa introspection
NSApplication
-respondsToSelector: YES
-setDelegate:
5.3
Cocoa NSNotifi cation
NSEvent
AppKit
124 5
Objective-C
NSEvent
target action message
5.3.1
-sendEvent:
NSApplication
NSEvent Cocoa
OS X 10.6 Apple
NSEvent -addLocalMonitorForEventsMatchingMask:handler:
NSApplication -sendEvent:
NSWindow
fi rst reponder
5.1 Interface Builder nib
First Responder action
target target/action
5.2
text view
Interface Builder outline view 5.3
1255.3
bordered scroll view
split view
NSDocument NSDcoumentController
5.1 nib
5.2
126 5
5.3 5.2
NSApplication NSWindow -sendEvent:
- (void)sendEvent:(NSEvent *)event{
if ([event type] == NSKeyDown){
// Intercept eventsreturn;
}[super sendEvent: event];
}
Keynote
1275.3
5.3.2
- target-action
sender Interface Builder
Cocoa
#define IBAction void
- (IBAction)doSomething: (id)sender;
IBAction void
Interface Builder
NSResponder
doSomething:
NSApplications -sendAction:to:from
key window
main window
-respondsToSelector:
NSApp
-targetForAction:
NSApp
nil
128 5
5.3.3
NSView -accetsFirstResponder YES
-becomeFirstResponder
YES
-resignFirstResponder NO
NSWindow -makeFirstResponder:
5.4 Run Loop
run loop NSRunLoop
Foundation run loop
Web AppKit
Foundation run-loop run-loop
NeXTSTEP Mach run loop Mach
Mach
Mach
I/O
TCP IP
1295.4 Run Loop
Mach 10
Mach
4BSD Mach
OS X NeXTSTEP NeXTSTEP Mach
IPC Mach
BSD
run-loop Mach
OpenStep
NSPort
window server
NSPort
Mach
run loop
- (void)addPort: (NSPort*)aPort forMode: (NSString*)mode;- (void)removePort: (NSPort*)aPort forMode: (NSString*)mode;
run
loop Foundation NSDefaultRunLoopMode
NSConnectionReplyMode
run loop
AppKit Foundation Foundation
run-loop
AppKit
NSModalPanelRunLoopMode NSEventTrackingRunLoopMode
NSApp run loop
5.4 Cocoa
130 5
Mach 1
run loop NSEvent NSApp -sendEvent:
NSPortWindow Server NSApplication
NSWindowNSView
(First responder)NSView
(superview)
Mach Message NSPortMessage
NSRunLoop
NSEvent
NSEvent
NSEvent
Controller
NSEventNSNotification
5.4 Cocao
NSResponder
Objective-C
Cocoa run loop NSAlert
run loop
alert -runModal
alert
- (void)beginSheetModalForWindow: (NSWindow*)windowmodalDelegate: (id)modalDelegate
didEndSelector: (SEL)alertDidEndSelectorcontextInfo: (void*)contextInfo
-
1 IPC
1315.5
alert
contextInfo
NSOpenPanel
Hack
NSTimer
run-loop run loop
run loop
+ (NSTimer*)scheduledTimerWithTimeInterval: (NSTimeInterval)secondstarget: (id)target
selector: (SEL)aSelectoruserInfo: (id)userInforepeats: (BOOL)repeats;
repeats YES
NSTimeInterval
double
Cocoa
NSTimer -userInfo
5.5
Cocoa -setDelegate:
Interface Builder Interface Builder
Control 5.5
project window
132 5
5.5 Interface Builder
outlet
5.6 delegate
5.6 Interface Builder
NSControl Cocoa
- (BOOL)control: (NSControl*)controltextShouldBeginEditing: (NSText*)fieldEditor;- (void)controlTextDidBeginEditing: (NSNotification*)aNotification;
void
NSControlTextDidBeginEditingNotifi cation
1335.5
GUI Cocoa
cell
cell
NSTableView
NSTableDataSource
- (NSInteger)numberOfRowsInTableView: (NSTableView*)aTableView;- (id)tableView: (NSTableView*)aTableViewobjectValueForTableColumn: (NSTableColumn*)aTableColumn
row: (NSInteger)rowIndex;
- (void)tableView: (NSTableView*)aTableViewsetObjectValue: (id)anObjectforTableColumn: (NSTableColumn*)aTableColumn
row: (NSInteger)rowIndex;
NSComboBox
auto-completion
134 5
11
5.6
5.6.1
CPU
Cocoa NSScreen
OS X
CPU GPU
Cocoa NSWindows
NSView
Cocoa
AppKit NSWindow NSPanel
Cocoa
1355.6
NSWindow
-hidesOnDeactive
NO YES
OS X
NSWindowController
nib
NSWindowController -initWithWindowNibName:
nib owner
nib File's owner
5.6.2
Cocoa NSView Objective-C
NSResponder
NSResponder NSView
-drawRect: NSRect
(0, 0)
-drawRect:
Cocoa
Mac
� NSButton� NSImageView� NSTextField NSTextView� NSMovieView QuickTime
136 5
� NSOpenGLView OpenGL OpenGL API
Cocoa
Interface Builder
Interface Builder palette
NSButton
5.6.3
100
10.5 32 NSView 80
64 152 16KB
100 100 1.5MB
NSView
NSCell
Leopard NSCell 20 32 32 64
100 100 1MB
Cocoa PostScript Cocoa
PostScript 72 PostScript OS X
72dpi
150dpi
-setObjectValue: -drawWithFrame:inView:
NSControl
1375.7
Cocoa
NSButton NSControl
NSButtonCell NSMatrix
NSCell NSControl
UI
5.7
AppKit Cocoa
AppKit Foundation
Cocoa