diff options
Diffstat (limited to 'examples/cross_calculator/ios/src')
-rw-r--r-- | examples/cross_calculator/ios/src/AppDelegate.h | 7 | ||||
-rw-r--r-- | examples/cross_calculator/ios/src/AppDelegate.m | 39 | ||||
-rw-r--r-- | examples/cross_calculator/ios/src/NRViewController.h | 11 | ||||
-rw-r--r-- | examples/cross_calculator/ios/src/NRViewController.m | 210 | ||||
-rw-r--r-- | examples/cross_calculator/ios/src/cross-calculator-Prefix.pch | 10 | ||||
-rw-r--r-- | examples/cross_calculator/ios/src/main.m | 13 |
6 files changed, 290 insertions, 0 deletions
diff --git a/examples/cross_calculator/ios/src/AppDelegate.h b/examples/cross_calculator/ios/src/AppDelegate.h new file mode 100644 index 000000000..a5a8b3852 --- /dev/null +++ b/examples/cross_calculator/ios/src/AppDelegate.h @@ -0,0 +1,7 @@ +#import <UIKit/UIKit.h> + +@interface AppDelegate : UIResponder <UIApplicationDelegate> + +@property (strong, nonatomic) UIWindow *window; + +@end diff --git a/examples/cross_calculator/ios/src/AppDelegate.m b/examples/cross_calculator/ios/src/AppDelegate.m new file mode 100644 index 000000000..53e7f6188 --- /dev/null +++ b/examples/cross_calculator/ios/src/AppDelegate.m @@ -0,0 +1,39 @@ +#import "AppDelegate.h" + +#import "NRViewController.h" + + +@interface AppDelegate () +@property (nonatomic, retain) NRViewController *viewController; +@end + + +@implementation AppDelegate + +@synthesize viewController = _viewController; +@synthesize window = _window; + +- (BOOL)application:(UIApplication *)application + didFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + self.window = [[[UIWindow alloc] + initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; + + self.viewController = [[NRViewController new] autorelease]; + if ([self.window respondsToSelector:@selector(setRootViewController:)]) + self.window.rootViewController = self.viewController; + else + [self.window addSubview:self.viewController.view]; + [self.window makeKeyAndVisible]; + + return YES; +} + +- (void)dealloc +{ + [_window release]; + [_viewController release]; + [super dealloc]; +} + +@end diff --git a/examples/cross_calculator/ios/src/NRViewController.h b/examples/cross_calculator/ios/src/NRViewController.h new file mode 100644 index 000000000..36ba37758 --- /dev/null +++ b/examples/cross_calculator/ios/src/NRViewController.h @@ -0,0 +1,11 @@ +@interface NRViewController : UIViewController + +@property (nonatomic, retain) IBOutlet UIButton *calculateButton; +@property (nonatomic, retain) IBOutlet UITextField *aText; +@property (nonatomic, retain) IBOutlet UITextField *bText; +@property (nonatomic, retain) IBOutlet UILabel *resultLabel; + +- (IBAction)calculateButtonTouched; +- (IBAction)backgroundTouched; + +@end \ No newline at end of file diff --git a/examples/cross_calculator/ios/src/NRViewController.m b/examples/cross_calculator/ios/src/NRViewController.m new file mode 100644 index 000000000..bd0df7d6b --- /dev/null +++ b/examples/cross_calculator/ios/src/NRViewController.m @@ -0,0 +1,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 diff --git a/examples/cross_calculator/ios/src/cross-calculator-Prefix.pch b/examples/cross_calculator/ios/src/cross-calculator-Prefix.pch new file mode 100644 index 000000000..2f331ed43 --- /dev/null +++ b/examples/cross_calculator/ios/src/cross-calculator-Prefix.pch @@ -0,0 +1,10 @@ +#import <Availability.h> + +#ifndef __IPHONE_3_0 +#warning "This project uses features only available in iOS SDK 3.0 and later." +#endif + +#ifdef __OBJC__ + #import <UIKit/UIKit.h> + #import <Foundation/Foundation.h> +#endif diff --git a/examples/cross_calculator/ios/src/main.m b/examples/cross_calculator/ios/src/main.m new file mode 100644 index 000000000..7866684fe --- /dev/null +++ b/examples/cross_calculator/ios/src/main.m @@ -0,0 +1,13 @@ +#import <UIKit/UIKit.h> + +#import "AppDelegate.h" +#import "backend.h" + +int main(int argc, char *argv[]) +{ + @autoreleasepool { + NimMain(); + return UIApplicationMain(argc, argv, nil, + NSStringFromClass([AppDelegate class])); + } +} |