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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#import "NRViewController.h"
#import "backend.h"
@implementation NRViewController
@synthesize aText = _aText;
@synthesize bText = _bText;
@synthesize calculateButton = _calculateButton;
@synthesize resultLabel = _resultLabel;
- (void)dealloc
{
[_aText release];
[_bText release];
[_calculateButton release];
[_resultLabel release];
[super dealloc];
}
- (void)viewDidUnload
{
self.calculateButton = nil;
self.aText = nil;
self.bText = nil;
self.resultLabel = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
/// User wants to calculate the inputs. Well, do it!
- (IBAction)calculateButtonTouched
{
// Dismiss all keyboards.
[self backgroundTouched];
// Call nimrod code, store the result and display it.
const int a = [self.aText.text intValue];
const int b = [self.bText.text intValue];
const int c = myAdd(a, b);
self.resultLabel.text = [NSString stringWithFormat:@"%d + %d = %d",
a, b, c];
}
/// If the user touches the background, dismiss any visible keyboard.
- (IBAction)backgroundTouched
{
[self.aText resignFirstResponder];
[self.bText resignFirstResponder];
}
/** Custom loadView method for backwards compatiblity.
* Unfortunately I've been unable to coerce Xcode 4.4 to generate nib files
* which are compatible with my trusty iOS 3.0 ipod touch so in order to be
* fully compatible for all devices we have to build the interface manually in
* code rather than through the keyed archivers provided by the interface
* builder.
*
* Rather than recreating the user interface manually in code the tool nib2obj
* was used on the xib file and slightly modified to fit the original property
* names. Which means here is a lot of garbage you would never write in real
* life. Please ignore the following "wall of code" for the purposes of
* learning Nimrod, this is all just because Apple can't be bothered to
* maintain backwards compatibility properly.
*/
- (void)loadView
{
[super loadView];
self.calculateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.calculateButton.autoresizesSubviews = YES;
self.calculateButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
self.calculateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
self.calculateButton.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
self.calculateButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.calculateButton.frame = CGRectMake(193.0, 124.0, 107.0, 37.0);
self.calculateButton.tag = 5;
[self.calculateButton setTitle:@"Add!" forState:UIControlStateNormal];
[self.calculateButton addTarget:self
action:@selector(calculateButtonTouched)
forControlEvents:UIControlEventTouchUpInside];
UILabel *label11 = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 124.0, 60.0, 37.0)];
label11.adjustsFontSizeToFitWidth = YES;
label11.autoresizesSubviews = YES;
label11.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
label11.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
label11.frame = CGRectMake(20.0, 124.0, 60.0, 37.0);
label11.tag = 6;
label11.text = @"Result:";
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 34.0)];
label4.adjustsFontSizeToFitWidth = YES;
label4.autoresizesSubviews = YES;
label4.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
label4.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
label4.frame = CGRectMake(0.0, 0.0, 320.0, 34.0);
label4.tag = 2;
label4.text = @"Nimrod Crossplatform Calculator";
label4.textAlignment = UITextAlignmentCenter;
UIButton *background_button = [UIButton buttonWithType:UIButtonTypeCustom];
background_button.autoresizesSubviews = YES;
background_button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
background_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
background_button.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
background_button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
background_button.frame = CGRectMake(0.0, -10.0, 320.0, 480.0);
background_button.tag = 1;
[background_button addTarget:self action:@selector(backgroundTouched)
forControlEvents:UIControlEventTouchDown];
self.resultLabel = [[[UILabel alloc] initWithFrame:CGRectMake(88.0, 124.0, 97.0, 37.0)] autorelease];
self.resultLabel.adjustsFontSizeToFitWidth = YES;
self.resultLabel.autoresizesSubviews = YES;
self.resultLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
self.resultLabel.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
self.resultLabel.frame = CGRectMake(88.0, 124.0, 97.0, 37.0);
self.resultLabel.tag = 7;
self.resultLabel.text = @"";
self.aText = [[[UITextField alloc] initWithFrame:CGRectMake(193.0, 42.0, 107.0, 31.0)] autorelease];
self.aText.adjustsFontSizeToFitWidth = YES;
self.aText.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.aText.autocorrectionType = UITextAutocorrectionTypeDefault;
self.aText.autoresizesSubviews = YES;
self.aText.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
self.aText.borderStyle = UITextBorderStyleRoundedRect;
self.aText.clearButtonMode = UITextFieldViewModeWhileEditing;
self.aText.clearsOnBeginEditing = NO;
self.aText.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.aText.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
self.aText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.aText.enablesReturnKeyAutomatically = NO;
self.aText.frame = CGRectMake(193.0, 42.0, 107.0, 31.0);
self.aText.keyboardAppearance = UIKeyboardAppearanceDefault;
self.aText.keyboardType = UIKeyboardTypeNumberPad;
self.aText.placeholder = @"Integer";
self.aText.returnKeyType = UIReturnKeyDefault;
self.aText.tag = 8;
self.aText.text = @"";
self.aText.textAlignment = UITextAlignmentCenter;
UILabel *label7 = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 42.0, 165.0, 31.0)];
label7.adjustsFontSizeToFitWidth = YES;
label7.autoresizesSubviews = YES;
label7.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
label7.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
label7.frame = CGRectMake(20.0, 42.0, 165.0, 31.0);
label7.tag = 3;
label7.text = @"Value A:";
UILabel *label8 = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 81.0, 165.0, 31.0)];
label8.adjustsFontSizeToFitWidth = YES;
label8.autoresizesSubviews = YES;
label8.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
label8.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
label8.frame = CGRectMake(20.0, 81.0, 165.0, 31.0);
label8.tag = 4;
label8.text = @"Value B:";
self.bText = [[[UITextField alloc]
initWithFrame:CGRectMake(193.0, 81.0, 107.0, 31.0)] autorelease];
self.bText.adjustsFontSizeToFitWidth = YES;
self.bText.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.bText.autocorrectionType = UITextAutocorrectionTypeDefault;
self.bText.autoresizesSubviews = YES;
self.bText.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
self.bText.borderStyle = UITextBorderStyleRoundedRect;
self.bText.clearButtonMode = UITextFieldViewModeWhileEditing;
self.bText.clearsOnBeginEditing = NO;
self.bText.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.bText.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
self.bText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.bText.enablesReturnKeyAutomatically = NO;
self.bText.frame = CGRectMake(193.0, 81.0, 107.0, 31.0);
self.bText.keyboardAppearance = UIKeyboardAppearanceDefault;
self.bText.keyboardType = UIKeyboardTypeNumberPad;
self.bText.placeholder = @"Integer";
self.bText.returnKeyType = UIReturnKeyDefault;
self.bText.tag = 9;
self.bText.text = @"";
self.bText.textAlignment = UITextAlignmentCenter;
self.view.frame = CGRectMake(0.0, 20.0, 320.0, 460.0);
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor colorWithWhite:1.000 alpha:1.000];
self.view.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}");
self.view.frame = CGRectMake(0.0, 20.0, 320.0, 460.0);
self.view.tag = 0;
[self.view addSubview:background_button];
[self.view addSubview:label4];
[self.view addSubview:label7];
[self.view addSubview:label8];
[self.view addSubview:self.calculateButton];
[self.view addSubview:label11];
[self.view addSubview:self.resultLabel];
[self.view addSubview:self.aText];
[self.view addSubview:self.bText];
}
@end
|