blob: 2ab0329af5786f75046feb320e0f9ac2790f3ae5 [file] [log] [blame] [view]
andybons6eaa0c0d2015-08-26 20:12:521# Cocoa Tips and Tricks
andybons3322f762015-08-24 21:37:092
andybons6eaa0c0d2015-08-26 20:12:523A collection of idioms that we use when writing the Cocoa views and controllers
4for Chromium.
5
6[TOC]
andybons3322f762015-08-24 21:37:097
8## NSWindowController Initialization
9
andybons6eaa0c0d2015-08-26 20:12:5210To make sure that |window| and |delegate| are wired up correctly in your xib,
11it's useful to add this to your window controller:
andybons3322f762015-08-24 21:37:0912
andybons6eaa0c0d2015-08-26 20:12:5213```objective-c
andybons3322f762015-08-24 21:37:0914- (void)awakeFromNib {
15 DCHECK([self window]);
16 DCHECK_EQ(self, [[self window] delegate]);
17}
18```
19
20## NSWindowController Cleanup
21
andybons6eaa0c0d2015-08-26 20:12:5222"You want the window controller to release itself it |-windowDidClose:|, because
23else it could die while its views are still around. if it (auto)releases itself
24in the callback, the window and its views are already gone and they won't send
25messages to the released controller."
andybons3322f762015-08-24 21:37:0926- Nico Weber (thakis@)
27
andybons6eaa0c0d2015-08-26 20:12:5228See
29[Window Closing Behavior, ADC Reference](http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Documents/Concepts/WindowClosingBehav.html#//apple_ref/doc/uid/20000027)
30for the full story.
andybons3322f762015-08-24 21:37:0931
32What this means in practice is:
33
andybons6eaa0c0d2015-08-26 20:12:5234```objective-c
andybons3322f762015-08-24 21:37:0935@interface MyWindowController : NSWindowController<NSWindowDelegate> {
36 IBOutlet NSButton* closeButton_;
37}
38- (IBAction)closeButton:(id)sender;
39@end
40
41@implementation MyWindowController
42- (id)init {
43 if ((self = [super initWithWindowNibName:@"MyWindow" ofType:@"nib"])) {
44 }
45 return self;
46}
47
48- (void)awakeFromNib {
49 // Check that we set the window and its delegate in the XIB.
50 DCHECK([self window]);
51 DCHECK_EQ(self, [[self window] delegate]);
52}
53
54// NSWindowDelegate notification.
55- (void)windowWillClose:(NSNotification*)notif {
56 [self autorelease];
57}
58
59// Action for a button that lets the user close the window.
60- (IBAction)closeButton:(id)sender {
61 // We clean ourselves up after the window has closed.
62 [self close];
63}
64@end
65```
66
67## Unit Tests
68
andybons6eaa0c0d2015-08-26 20:12:5269There are four Chromium-specific GTest macros for writing ObjC++ test cases.
70These macros are `EXPECT_NSEQ`, `EXPECT_NSNE`, and `ASSERT` variants by the same
71names. These test `-[id<NSObject> isEqual:]` and will print the object's
72`-description` in GTest-style if the assertion fails. These macros are defined
73in `//testing/gtest_mac.h`. Just include that file and you can start using them.
andybons3322f762015-08-24 21:37:0974
75This allows you to write this:
andybons6eaa0c0d2015-08-26 20:12:5276
77```objective-c
78EXPECT_NSEQ(@"foo", aString);
andybons3322f762015-08-24 21:37:0979```
andybons6eaa0c0d2015-08-26 20:12:5280
andybons3322f762015-08-24 21:37:0981Instead of this:
andybons6eaa0c0d2015-08-26 20:12:5282
83```objective-c
84EXPECT_TRUE([aString isEqualToString:@"foo"]);
andybons3322f762015-08-24 21:37:0985```