-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathButton.m
More file actions
78 lines (65 loc) · 2.42 KB
/
Button.m
File metadata and controls
78 lines (65 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <Cocoa/Cocoa.h>
@interface Window : NSWindow {
NSButton* button1;
NSButton* button2;
NSTextField* label1;
NSTextField* label2;
int button1Clicked;
int button2Clicked;
}
- (instancetype) init;
- (BOOL)windowShouldClose:(id)sender;
- (IBAction) OnButton1Click:(id)sender;
- (IBAction) OnButton2Click:(id)sender;
@end
@implementation Window
- (instancetype) init {
button1Clicked = 0;
button2Clicked = 0;
button1 = [[[NSButton alloc] initWithFrame:NSMakeRect(50, 225, 90, 25)] autorelease];
[button1 setTitle:@"button1"];
[button1 setBezelStyle:NSBezelStyleRounded];
[button1 setTarget:self];
[button1 setAction:@selector(OnButton1Click:)];
[button1 setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
button2 = [[[NSButton alloc] initWithFrame:NSMakeRect(50, 125, 200, 75)] autorelease];
[button2 setTitle:@"button2"];
[button2 setBezelStyle:NSBezelStyleRegularSquare];
[button2 setTarget:self];
[button2 setAction:@selector(OnButton2Click:)];
[button2 setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
label1 = [[NSTextField alloc] initWithFrame:NSMakeRect(50, 80, 200, 20)];
[label1 setStringValue:@"button1 clicked 0 times"];
[label1 setBezeled:NO];
[label1 setDrawsBackground:NO];
[label1 setEditable:NO];
label2 = [[NSTextField alloc] initWithFrame:NSMakeRect(50, 50, 200, 20)];
[label2 setStringValue:@"button2 clicked 0 times"];
[label2 setBezeled:NO];
[label2 setDrawsBackground:NO];
[label2 setEditable:NO];
[super initWithContentRect:NSMakeRect(100, 100, 300, 300) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
[self setTitle:@"Button example"];
[[self contentView] addSubview:button1];
[[self contentView] addSubview:button2];
[[self contentView] addSubview:label1];
[[self contentView] addSubview:label2];
[self setIsVisible:YES];
return self;
}
- (BOOL)windowShouldClose:(id)sender {
[NSApp terminate:sender];
return YES;
}
- (IBAction) OnButton1Click:(id)sender {
[label1 setStringValue:[NSString stringWithFormat:@"button1 clicked %d times", ++button1Clicked]];
}
- (IBAction) OnButton2Click:(id)sender {
[label2 setStringValue:[NSString stringWithFormat:@"button2 clicked %d times", ++button2Clicked]];
}
@end
int main(int argc, char* argv[]) {
[NSApplication sharedApplication];
[[[[Window alloc] init] autorelease] makeMainWindow];
[NSApp run];
}