Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FirstViewController.h
- // Randomizer
- //
- // Created by John Anderson on 7/17/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface FirstViewController : UIViewController <UITextFieldDelegate>{
- IBOutlet UITextField *tF1;
- IBOutlet UITextField *tF2;
- IBOutlet UIButton *button;
- IBOutlet UILabel *rNL;
- }
- @property (nonatomic, retain) IBOutlet UITextField *tF1;
- @property (nonatomic, retain) IBOutlet UITextField *tF2;
- @property (nonatomic, retain) IBOutlet UIButton *button;
- @property (nonatomic, retain) IBOutlet UILabel *rNL;
- -(IBAction) tF1DidEnd: (id) sender;
- -(IBAction) tF2DidEnd: (id) sender;
- -(IBAction) button: (id) sender;
- @end
- //
- // FirstViewController.m
- // Randomizer
- //
- // Created by John Anderson on 7/17/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "FirstViewController.h"
- int r = 1;
- int n = 1;
- int nn = 1;
- @implementation FirstViewController
- @synthesize tF1, tF2, button, rNL;
- - (void)viewDidLoad {
- [super viewDidLoad];
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardDidShow:)
- name:UIKeyboardDidShowNotification
- object:nil];
- } else {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillShow:)
- name:UIKeyboardWillShowNotification
- object:nil];
- }
- tF1.borderStyle = UITextBorderStyleRoundedRect;
- tF1.keyboardType = UIKeyboardTypeNumberPad;
- tF1.returnKeyType = UIReturnKeyDone;
- tF1.textAlignment = UITextAlignmentLeft;
- tF2.borderStyle = UITextBorderStyleRoundedRect;
- tF2.keyboardType = UIKeyboardTypeNumberPad;
- tF2.returnKeyType = UIReturnKeyDone;
- tF2.textAlignment = UITextAlignmentLeft;
- }
- -(IBAction) tF1DidEnd: (id) sender
- {
- //The direct approach
- //We've linked this method to text field one in IB
- //so we know which text field to reference
- //[tF1 resignFirstResponder];
- int n = (int)[tF1 text];
- NSLog(@"doneButton");
- }
- -(IBAction) tF2DidEnd: (id) sender
- {
- //The direct approach
- //We've linked this method to text field one in IB
- //so we know which text field to reference
- //[tF2 resignFirstResponder];
- int nn = (int)[tF2 text];
- NSLog(@"doneButton");
- }
- - (void)addButtonToKeyboard {
- // create custom button
- UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
- doneButton.frame = CGRectMake(0, 163, 106, 53);
- doneButton.adjustsImageWhenHighlighted = NO;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
- [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
- [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
- } else {
- [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
- [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
- }
- [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
- // locate keyboard view
- UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
- UIView* keyboard;
- for(int i=0; i<[tempWindow.subviews count]; i++) {
- keyboard = [tempWindow.subviews objectAtIndex:i];
- // keyboard found, add the button
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
- if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
- [keyboard addSubview:doneButton];
- } else {
- if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
- [keyboard addSubview:doneButton];
- }
- }
- }
- - (void)keyboardWillShow:(NSNotification *)note {
- // if clause is just an additional precaution, you could also dismiss it
- if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
- [self addButtonToKeyboard];
- }
- }
- - (void)keyboardDidShow:(NSNotification *)note {
- // if clause is just an additional precaution, you could also dismiss it
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
- [self addButtonToKeyboard];
- }
- }
- - (void)doneButton:(id)sender {
- NSLog(@"doneButton");
- //NSLog(@"Input: %@", textField.text);
- [tF1 resignFirstResponder];
- [tF2 resignFirstResponder];
- }
- -(IBAction) button: (id) sender
- {
- if (n == nn) {
- UIAlertView *sameNumbers = [[UIAlertView alloc]
- initWithTitle:@"Error"
- message:@"It's not really random when the numbers are the same, now is it?"
- delegate:self
- cancelButtonTitle:@"Okay"
- otherButtonTitles:nil];
- [sameNumbers show];
- [sameNumbers release];
- }
- else {
- if (n > nn) {
- int r = (arc4random() % n) + nn;
- }
- else {
- int r = (arc4random() % nn) + n;
- }
- }
- int r = [[rNL text] intValue];
- }
- /*-(IBAction) aTextFieldDidEnd: (id) sender
- {
- //The indirect approach - use a pointer to the sender object
- //Use this as a generic method to process returns from
- //a collection of text fields
- //Add extra code to select between them using position, or some other unique attribute
- UITextField *thisTextField = (UITextField *)sender;
- [thisTextField resignFirstResponder];
- //There's only one possible text field in this trivial example, so we know which label to update
- labelTwo.text = thisTextField.text;
- }*/
- /*
- // The designated initializer. Override to perform setup that is required before the view is loaded.
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- */
- /*
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView {
- }
- */
- /*
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- */
- /*
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- */
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)dealloc {
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement