Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /docs /cocoa_tips_and_tricks.md
blob: 2ab0329af5786f75046feb320e0f9ac2790f3ae5 [file] [log] [blame] [view]
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]1# Cocoa Tips and Tricks
andybons3322f762015-08-24 21:37:09[diff] [blame]2
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]3A collection of idioms that weusewhen writing theCocoa viewsand controllers
4forChromium.
5
6[TOC]
andybons3322f762015-08-24 21:37:09[diff] [blame]7
8## NSWindowController Initialization
9
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]10To make sure that|window|and|delegate| are wired up correctlyin your xib,
11it's useful to add this to your window controller:
andybons3322f762015-08-24 21:37:09[diff] [blame]12
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]13```objective-c
andybons3322f762015-08-24 21:37:09[diff] [blame]14- (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:52[diff] [blame]22"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:09[diff] [blame]26- Nico Weber (thakis@)
27
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]28See
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:09[diff] [blame]31
32What this means in practice is:
33
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]34```objective-c
andybons3322f762015-08-24 21:37:09[diff] [blame]35@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:52[diff] [blame]69There 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:09[diff] [blame]74
75This allows you to write this:
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]76
77```objective-c
78EXPECT_NSEQ(@"foo", aString);
andybons3322f762015-08-24 21:37:09[diff] [blame]79```
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]80
andybons3322f762015-08-24 21:37:09[diff] [blame]81Instead of this:
andybons6eaa0c0d2015-08-26 20:12:52[diff] [blame]82
83```objective-c
84EXPECT_TRUE([aString isEqualToString:@"foo"]);
andybons3322f762015-08-24 21:37:09[diff] [blame]85```

[8]ページ先頭

©2009-2025 Movatter.jp