Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  FirstViewController.h
  3. //  Randomizer
  4. //
  5. //  Created by John Anderson on 7/17/11.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11.  
  12. @interface FirstViewController : UIViewController <UITextFieldDelegate>{
  13.    
  14.     IBOutlet UITextField *tF1;
  15.     IBOutlet UITextField *tF2;
  16.     IBOutlet UIButton *button;
  17.     IBOutlet UILabel *rNL;
  18. }
  19.  
  20. @property (nonatomic, retain) IBOutlet UITextField *tF1;
  21. @property (nonatomic, retain) IBOutlet UITextField *tF2;
  22. @property (nonatomic, retain) IBOutlet UIButton *button;
  23. @property (nonatomic, retain) IBOutlet UILabel *rNL;
  24.  
  25. -(IBAction) tF1DidEnd: (id) sender;
  26. -(IBAction) tF2DidEnd: (id) sender;
  27. -(IBAction) button: (id) sender;
  28.  
  29. @end
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. //
  45. //  FirstViewController.m
  46. //  Randomizer
  47. //
  48. //  Created by John Anderson on 7/17/11.
  49. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  50. //
  51.  
  52. #import "FirstViewController.h"
  53.  
  54. int r = 1;
  55. int n = 1;
  56. int nn = 1;
  57.  
  58.  
  59. @implementation FirstViewController
  60.  
  61. @synthesize tF1, tF2, button, rNL;
  62.  
  63. - (void)viewDidLoad {
  64.     [super viewDidLoad];
  65.    
  66.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
  67.         [[NSNotificationCenter defaultCenter] addObserver:self
  68.                                                  selector:@selector(keyboardDidShow:)
  69.                                                      name:UIKeyboardDidShowNotification
  70.                                                    object:nil];    
  71.     } else {
  72.         [[NSNotificationCenter defaultCenter] addObserver:self
  73.                                                  selector:@selector(keyboardWillShow:)
  74.                                                      name:UIKeyboardWillShowNotification
  75.                                                    object:nil];
  76.     }
  77.    
  78.     tF1.borderStyle = UITextBorderStyleRoundedRect;
  79.     tF1.keyboardType = UIKeyboardTypeNumberPad;
  80.     tF1.returnKeyType = UIReturnKeyDone;
  81.     tF1.textAlignment = UITextAlignmentLeft;
  82.    
  83.     tF2.borderStyle = UITextBorderStyleRoundedRect;
  84.     tF2.keyboardType = UIKeyboardTypeNumberPad;
  85.     tF2.returnKeyType = UIReturnKeyDone;
  86.     tF2.textAlignment = UITextAlignmentLeft;
  87.    
  88. }
  89.  
  90. -(IBAction) tF1DidEnd: (id) sender
  91. {
  92.     //The direct approach
  93.     //We've linked this method to text field one in IB
  94.     //so we know which text field to reference
  95.     //[tF1 resignFirstResponder];
  96.     int n = (int)[tF1 text];
  97.     NSLog(@"doneButton");
  98. }
  99.  
  100. -(IBAction) tF2DidEnd: (id) sender
  101. {
  102.     //The direct approach
  103.     //We've linked this method to text field one in IB
  104.     //so we know which text field to reference
  105.     //[tF2 resignFirstResponder];
  106.     int nn = (int)[tF2 text];
  107.     NSLog(@"doneButton");
  108. }
  109.  
  110. - (void)addButtonToKeyboard {
  111.     // create custom button
  112.     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  113.     doneButton.frame = CGRectMake(0, 163, 106, 53);
  114.     doneButton.adjustsImageWhenHighlighted = NO;
  115.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
  116.         [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
  117.         [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
  118.     } else {        
  119.         [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
  120.         [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
  121.     }
  122.     [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
  123.     // locate keyboard view
  124.     UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
  125.     UIView* keyboard;
  126.     for(int i=0; i<[tempWindow.subviews count]; i++) {
  127.         keyboard = [tempWindow.subviews objectAtIndex:i];
  128.         // keyboard found, add the button
  129.         if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
  130.             if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
  131.                 [keyboard addSubview:doneButton];
  132.         } else {
  133.             if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
  134.                 [keyboard addSubview:doneButton];
  135.         }
  136.     }
  137. }
  138.  
  139. - (void)keyboardWillShow:(NSNotification *)note {
  140.     // if clause is just an additional precaution, you could also dismiss it
  141.     if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
  142.         [self addButtonToKeyboard];
  143.     }
  144. }
  145.  
  146. - (void)keyboardDidShow:(NSNotification *)note {
  147.     // if clause is just an additional precaution, you could also dismiss it
  148.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
  149.         [self addButtonToKeyboard];
  150.     }
  151. }
  152.  
  153. - (void)doneButton:(id)sender {
  154.     NSLog(@"doneButton");
  155.     //NSLog(@"Input: %@", textField.text);
  156.     [tF1 resignFirstResponder];
  157.     [tF2 resignFirstResponder];
  158.    
  159. }
  160.  
  161. -(IBAction) button: (id) sender
  162. {
  163.     if (n == nn) {
  164.         UIAlertView *sameNumbers = [[UIAlertView alloc]
  165.                                     initWithTitle:@"Error"
  166.                                     message:@"It's not really random when the numbers are the same, now is it?"
  167.                                     delegate:self
  168.                                     cancelButtonTitle:@"Okay"
  169.                                     otherButtonTitles:nil];
  170.         [sameNumbers show];
  171.         [sameNumbers release];
  172.     }
  173.     else {
  174.         if (n > nn) {
  175.             int r = (arc4random() % n) + nn;
  176.         }
  177.         else {
  178.             int r = (arc4random() % nn) + n;
  179.         }
  180.        
  181.     }
  182.     int r = [[rNL text] intValue];
  183.    
  184.    
  185. }
  186.  
  187.  
  188.  
  189. /*-(IBAction) aTextFieldDidEnd: (id) sender
  190. {
  191.     //The indirect approach - use a pointer to the sender object
  192.     //Use this as a generic method to process returns from
  193.     //a collection of text fields
  194.     //Add extra code to select between them using position, or some other unique attribute
  195.     UITextField *thisTextField = (UITextField *)sender;
  196.     [thisTextField resignFirstResponder];
  197.     //There's only one possible text field in this trivial example, so we know which label to update
  198.     labelTwo.text = thisTextField.text;
  199. }*/
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /*
  211. // The designated initializer. Override to perform setup that is required before the view is loaded.
  212. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  213.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  214.     if (self) {
  215.         // Custom initialization
  216.     }
  217.     return self;
  218. }
  219. */
  220.  
  221. /*
  222. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  223. - (void)loadView {
  224. }
  225. */
  226.  
  227. /*
  228. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  229. - (void)viewDidLoad {
  230.     [super viewDidLoad];
  231. }
  232. */
  233.  
  234. /*
  235. // Override to allow orientations other than the default portrait orientation.
  236. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  237.     // Return YES for supported orientations
  238.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  239. }
  240. */
  241.  
  242. - (void)didReceiveMemoryWarning {
  243.     // Releases the view if it doesn't have a superview.
  244.     [super didReceiveMemoryWarning];
  245.    
  246.     // Release any cached data, images, etc that aren't in use.
  247. }
  248.  
  249. - (void)viewDidUnload {
  250.     // Release any retained subviews of the main view.
  251.     // e.g. self.myOutlet = nil;
  252. }
  253.  
  254.  
  255. - (void)dealloc {
  256.     [super dealloc];
  257. }
  258.  
  259. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement