OLD | NEW |
1 # Introduction | 1 # Cocoa Tips and Tricks |
2 | 2 |
3 A collection of idioms that we use when writing the Cocoa views and controllers
for Chromium. | 3 A collection of idioms that we use when writing the Cocoa views and controllers |
| 4 for Chromium. |
| 5 |
| 6 [TOC] |
4 | 7 |
5 ## NSWindowController Initialization | 8 ## NSWindowController Initialization |
6 | 9 |
7 To make sure that |window| and |delegate| are wired up correctly in your xib, it
's useful to add this to your window controller: | 10 To make sure that |window| and |delegate| are wired up correctly in your xib, |
| 11 it's useful to add this to your window controller: |
8 | 12 |
9 ``` | 13 ```objective-c |
10 - (void)awakeFromNib { | 14 - (void)awakeFromNib { |
11 DCHECK([self window]); | 15 DCHECK([self window]); |
12 DCHECK_EQ(self, [[self window] delegate]); | 16 DCHECK_EQ(self, [[self window] delegate]); |
13 } | 17 } |
14 ``` | 18 ``` |
15 | 19 |
16 ## NSWindowController Cleanup | 20 ## NSWindowController Cleanup |
17 | 21 |
18 "You want the window controller to release itself it |-windowDidClose:|, because
else it could die while its views are still around. if it (auto)releases itself
in the callback, the window and its views are already gone and they won't send
messages to the released controller." | 22 "You want the window controller to release itself it |-windowDidClose:|, because |
| 23 else it could die while its views are still around. if it (auto)releases itself |
| 24 in the callback, the window and its views are already gone and they won't send |
| 25 messages to the released controller." |
19 - Nico Weber (thakis@) | 26 - Nico Weber (thakis@) |
20 | 27 |
21 See [Window Closing Behavior, ADC Reference](http://developer.apple.com/mac/libr
ary/documentation/Cocoa/Conceptual/Documents/Concepts/WindowClosingBehav.html#//
apple_ref/doc/uid/20000027) for the full story. | 28 See |
| 29 [Window Closing Behavior, ADC Reference](http://developer.apple.com/mac/library/
documentation/Cocoa/Conceptual/Documents/Concepts/WindowClosingBehav.html#//appl
e_ref/doc/uid/20000027) |
| 30 for the full story. |
22 | 31 |
23 What this means in practice is: | 32 What this means in practice is: |
24 | 33 |
25 ``` | 34 ```objective-c |
26 @interface MyWindowController : NSWindowController<NSWindowDelegate> { | 35 @interface MyWindowController : NSWindowController<NSWindowDelegate> { |
27 IBOutlet NSButton* closeButton_; | 36 IBOutlet NSButton* closeButton_; |
28 } | 37 } |
29 - (IBAction)closeButton:(id)sender; | 38 - (IBAction)closeButton:(id)sender; |
30 @end | 39 @end |
31 | 40 |
32 @implementation MyWindowController | 41 @implementation MyWindowController |
33 - (id)init { | 42 - (id)init { |
34 if ((self = [super initWithWindowNibName:@"MyWindow" ofType:@"nib"])) { | 43 if ((self = [super initWithWindowNibName:@"MyWindow" ofType:@"nib"])) { |
35 } | 44 } |
(...skipping 14 matching lines...) Expand all Loading... |
50 // Action for a button that lets the user close the window. | 59 // Action for a button that lets the user close the window. |
51 - (IBAction)closeButton:(id)sender { | 60 - (IBAction)closeButton:(id)sender { |
52 // We clean ourselves up after the window has closed. | 61 // We clean ourselves up after the window has closed. |
53 [self close]; | 62 [self close]; |
54 } | 63 } |
55 @end | 64 @end |
56 ``` | 65 ``` |
57 | 66 |
58 ## Unit Tests | 67 ## Unit Tests |
59 | 68 |
60 There are four Chromium-specific GTest macros for writing ObjC++ test cases. Th
ese macros are EXPECT\_NSEQ, EXPECT\_NSNE, and ASSERT variants by the same names
. These test `-[id<NSObject> isEqual:]` and will print the object's `-descripti
on` in GTest-style if the assertion fails. These macros are defined in //testin
g/gtest\_mac.h. Just include that file and you can start using them. | 69 There are four Chromium-specific GTest macros for writing ObjC++ test cases. |
| 70 These macros are `EXPECT_NSEQ`, `EXPECT_NSNE`, and `ASSERT` variants by the same |
| 71 names. These test `-[id<NSObject> isEqual:]` and will print the object's |
| 72 `-description` in GTest-style if the assertion fails. These macros are defined |
| 73 in `//testing/gtest_mac.h`. Just include that file and you can start using them. |
61 | 74 |
62 This allows you to write this: | 75 This allows you to write this: |
| 76 |
| 77 ```objective-c |
| 78 EXPECT_NSEQ(@"foo", aString); |
63 ``` | 79 ``` |
64 EXPECT_NSEQ(@"foo", aString); | 80 |
| 81 Instead of this: |
| 82 |
| 83 ```objective-c |
| 84 EXPECT_TRUE([aString isEqualToString:@"foo"]); |
65 ``` | 85 ``` |
66 Instead of this: | |
67 ``` | |
68 EXPECT_TRUE([aString isEqualToString:@"foo"]); | |
69 ``` | |
OLD | NEW |