Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: wkerswell on Sep 26th, 2012  |  syntax: Objective C  |  size: 6.00 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ApplicationViewController.h
  2.  
  3.     #import <UIKit/UIKit.h>
  4.    
  5.     @interface ApplicationViewController : UIViewController
  6.     @property (weak, nonatomic) IBOutlet UITextField *personOneNameTextField;
  7.     @property (weak, nonatomic) IBOutlet UITextField *personTwoNameTextField;
  8.     @property (weak, nonatomic) IBOutlet UITextField *beerNumberTextField;
  9.  
  10.     - (IBAction)startDrinkingBTN:(UIButton *)sender;
  11.    
  12.     @end
  13.  
  14. ApplicationViewController.m
  15.  
  16.     #import "ApplicationViewController.h"
  17.    
  18.     @interface ApplicationViewController ()
  19.    
  20.     @end
  21.    
  22.     @implementation ApplicationViewController
  23.     @synthesize personOneNameTextField;
  24. @synthesize personTwoNameTextField;
  25. @synthesize beerNumberTextField;
  26.  
  27.  
  28. - (IBAction)startDrinkingBTN:(UIButton *)sender{
  29.     //Save the users input to use on another page.
  30.     NSString *numberOfBeers = self.beerNumberTextField.text;
  31.     NSString *personOneName = self.personOneNameTextField.text;
  32.     NSString *personTwoName = self.personTwoNameTextField.text;
  33.     NSArray *details = [NSArray arrayWithObjects:numberOfBeers,personOneName,personTwoName, nil];
  34.     [self saveInfo:details];
  35.    
  36.     NSArray *beerArray = [[Information sharedInstance] beerArray];
  37.     NSLog(@"SetArray:%@",beerArray);
  38.    
  39. }
  40.  
  41.  
  42.  
  43. -(void)saveInfo:(NSArray *)array{
  44.    
  45.     [[Information sharedInstance] setBeerArray:[NSArray arrayWithArray:array]];
  46.  
  47. }
  48.  
  49.    
  50.     - (void)viewDidLoad
  51.     {
  52.        
  53.         [super viewDidLoad];
  54.         // Do any additional setup after loading the view.
  55.     }
  56.  
  57.  
  58. DividerViewController.h
  59.  
  60.  
  61.     #import <UIKit/UIKit.h>
  62.    
  63.     @interface DividerViewController : UIViewController{
  64.     NSInteger totalBeersToBeDrunk;
  65.     NSInteger amountDrunk;
  66.     NSInteger drunkByPersonOne;
  67.     NSInteger drunkByPersonTwo;
  68. }
  69. @property (nonatomic,retain) NSArray *beerArray;
  70. @property (weak, nonatomic) IBOutlet UILabel *totalBeersLabel;
  71. @property (weak, nonatomic) IBOutlet UILabel *beersLeftLabel;
  72. @property (weak, nonatomic) IBOutlet UILabel *personOneNameLabel;
  73. @property (weak, nonatomic) IBOutlet UILabel *personTwoNameLabel;
  74. @property (weak, nonatomic) IBOutlet UILabel *personOneBeerCount;
  75. @property (weak, nonatomic) IBOutlet UILabel *personTwoBeerCount;
  76.  
  77. - (IBAction)personOneStepper:(UIStepper *)sender;
  78. - (IBAction)personTwoStepper:(UIStepper *)sender;
  79.    
  80.     @end
  81.  
  82.  
  83.  
  84. DividerViewController.m
  85.  
  86. #import "DividerViewController.h"
  87.  
  88. @interface DividerViewController ()
  89.  
  90. @end
  91.  
  92. @implementation DividerViewController
  93. @synthesize totalBeersLabel;
  94. @synthesize beersLeftLabel;
  95. @synthesize personOneNameLabel;
  96. @synthesize personOneBeerCount;
  97. @synthesize personTwoNameLabel;
  98. @synthesize personTwoBeerCount;
  99.  
  100. -(NSArray *)getArray
  101. {
  102.     //get beer array
  103.     NSArray *beerArray = [[Information sharedInstance] beerArray];
  104.     NSLog(@"GetArray:%@",beerArray);
  105.     return beerArray;
  106. }
  107.  
  108.  
  109. - (void)viewDidLoad
  110. {
  111.     //assign the total beers to a labe on the view.
  112.     NSArray *detials = [self getArray];
  113.    
  114.     totalBeersLabel.text = detials[0];
  115.    
  116.     //turn the string into an int
  117.     totalBeersToBeDrunk = [detials[0] intValue];
  118.    
  119.     //assign names to labels.
  120.     personOneNameLabel.text =detials[1];
  121.     personTwoNameLabel.text =detials[2];
  122.    
  123.     [super viewDidLoad];
  124.         // Do any additional setup after loading the view.
  125. }
  126.  
  127.  
  128. - (IBAction)personOneStepperAction:(UIStepper *)sender {
  129.     //check make sure all the beers have not been drunk
  130.     if (drunkByPersonOne + drunkByPersonTwo != totalBeersToBeDrunk ){
  131.            
  132.             //Change number of beers drunk by person one.
  133.             double value = [sender value];
  134.             [personOneLabel setText:[NSString stringWithFormat:@"%d", (int)value]];
  135.            
  136.             //set the value of the stepper to the beers drunk for person one.
  137.             int myInt = (int)value;
  138.             drunkByPersonOne = myInt;
  139.            
  140.             //check var
  141.             NSLog(@" %i",drunkByPersonOne);
  142.            
  143.             //check to make sure they havent finished there beers.
  144.             [self checkIfAllDrunk];
  145.     }else{
  146.         [self cantAddMoreThanDrunk];
  147.     }
  148.    
  149. }
  150.  
  151.  
  152. - (IBAction)personTwoStepperAction:(UIStepper *)sender {
  153.     //check make sure all the beers have not been drunk
  154.     if (drunkByPersonOne + drunkByPersonTwo != totalBeersToBeDrunk ){
  155.        
  156.             //Change number of beers drunk by person one.
  157.             double value = [sender value];
  158.             [personTwoLabel setText:[NSString stringWithFormat:@"%d", (int)value]];
  159.            
  160.             //set the value of the stepper to the beers drunk for person one.
  161.             int myInt = (int)value;
  162.             drunkByPersonTwo = myInt;
  163.            
  164.             //check var
  165.             NSLog(@" %i",drunkByPersonTwo);
  166.            
  167.             //check to make sure they havent finished there beers.
  168.             [self checkIfAllDrunk];
  169.        
  170.     }else{
  171.         [self cantAddMoreThanDrunk];
  172.     }
  173. }
  174.  
  175. -(void)checkIfAllDrunk{
  176.     if (drunkByPersonOne + drunkByPersonTwo == totalBeersToBeDrunk){
  177.         NSLog(@"All beers drunk");
  178.         [self finishedBeers];
  179.     }
  180.  
  181.  
  182. }
  183.  
  184.  
  185.  
  186. -(void)finishedBeers{
  187.     //Alerts the user that they have finished and how many each person has had.
  188.     NSString * errorString = [NSString stringWithFormat:@"You Have finished all your beers :/ \n %@ had %d \n %@ had %d" , personOneName.text, drunkByPersonOne, personTwoName.text, drunkByPersonTwo];
  189.     UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:nil message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  190.     [errorAlert show];
  191.  
  192. }
  193.  
  194. -(void)cantAddMoreThanDrunk{
  195.     //alerts the user that they have finished and can not add anymore beers.
  196.     NSString * errorString = [NSString stringWithFormat:@"You Have finished all your beers. \n You cant add anymore."];
  197.     UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:nil message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  198.     [errorAlert show];
  199.  
  200.  
  201. }