Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ApplicationViewController.h
- #import <UIKit/UIKit.h>
- @interface ApplicationViewController : UIViewController
- @property (weak, nonatomic) IBOutlet UITextField *personOneNameTextField;
- @property (weak, nonatomic) IBOutlet UITextField *personTwoNameTextField;
- @property (weak, nonatomic) IBOutlet UITextField *beerNumberTextField;
- - (IBAction)startDrinkingBTN:(UIButton *)sender;
- @end
- ApplicationViewController.m
- #import "ApplicationViewController.h"
- @interface ApplicationViewController ()
- @end
- @implementation ApplicationViewController
- @synthesize personOneNameTextField;
- @synthesize personTwoNameTextField;
- @synthesize beerNumberTextField;
- - (IBAction)startDrinkingBTN:(UIButton *)sender{
- //Save the users input to use on another page.
- NSString *numberOfBeers = self.beerNumberTextField.text;
- NSString *personOneName = self.personOneNameTextField.text;
- NSString *personTwoName = self.personTwoNameTextField.text;
- NSArray *details = [NSArray arrayWithObjects:numberOfBeers,personOneName,personTwoName, nil];
- [self saveInfo:details];
- NSArray *beerArray = [[Information sharedInstance] beerArray];
- NSLog(@"SetArray:%@",beerArray);
- }
- -(void)saveInfo:(NSArray *)array{
- [[Information sharedInstance] setBeerArray:[NSArray arrayWithArray:array]];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- }
- DividerViewController.h
- #import <UIKit/UIKit.h>
- @interface DividerViewController : UIViewController{
- NSInteger totalBeersToBeDrunk;
- NSInteger amountDrunk;
- NSInteger drunkByPersonOne;
- NSInteger drunkByPersonTwo;
- }
- @property (nonatomic,retain) NSArray *beerArray;
- @property (weak, nonatomic) IBOutlet UILabel *totalBeersLabel;
- @property (weak, nonatomic) IBOutlet UILabel *beersLeftLabel;
- @property (weak, nonatomic) IBOutlet UILabel *personOneNameLabel;
- @property (weak, nonatomic) IBOutlet UILabel *personTwoNameLabel;
- @property (weak, nonatomic) IBOutlet UILabel *personOneBeerCount;
- @property (weak, nonatomic) IBOutlet UILabel *personTwoBeerCount;
- - (IBAction)personOneStepper:(UIStepper *)sender;
- - (IBAction)personTwoStepper:(UIStepper *)sender;
- @end
- DividerViewController.m
- #import "DividerViewController.h"
- @interface DividerViewController ()
- @end
- @implementation DividerViewController
- @synthesize totalBeersLabel;
- @synthesize beersLeftLabel;
- @synthesize personOneNameLabel;
- @synthesize personOneBeerCount;
- @synthesize personTwoNameLabel;
- @synthesize personTwoBeerCount;
- -(NSArray *)getArray
- {
- //get beer array
- NSArray *beerArray = [[Information sharedInstance] beerArray];
- NSLog(@"GetArray:%@",beerArray);
- return beerArray;
- }
- - (void)viewDidLoad
- {
- //assign the total beers to a labe on the view.
- NSArray *detials = [self getArray];
- totalBeersLabel.text = detials[0];
- //turn the string into an int
- totalBeersToBeDrunk = [detials[0] intValue];
- //assign names to labels.
- personOneNameLabel.text =detials[1];
- personTwoNameLabel.text =detials[2];
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- }
- - (IBAction)personOneStepperAction:(UIStepper *)sender {
- //check make sure all the beers have not been drunk
- if (drunkByPersonOne + drunkByPersonTwo != totalBeersToBeDrunk ){
- //Change number of beers drunk by person one.
- double value = [sender value];
- [personOneLabel setText:[NSString stringWithFormat:@"%d", (int)value]];
- //set the value of the stepper to the beers drunk for person one.
- int myInt = (int)value;
- drunkByPersonOne = myInt;
- //check var
- NSLog(@" %i",drunkByPersonOne);
- //check to make sure they havent finished there beers.
- [self checkIfAllDrunk];
- }else{
- [self cantAddMoreThanDrunk];
- }
- }
- - (IBAction)personTwoStepperAction:(UIStepper *)sender {
- //check make sure all the beers have not been drunk
- if (drunkByPersonOne + drunkByPersonTwo != totalBeersToBeDrunk ){
- //Change number of beers drunk by person one.
- double value = [sender value];
- [personTwoLabel setText:[NSString stringWithFormat:@"%d", (int)value]];
- //set the value of the stepper to the beers drunk for person one.
- int myInt = (int)value;
- drunkByPersonTwo = myInt;
- //check var
- NSLog(@" %i",drunkByPersonTwo);
- //check to make sure they havent finished there beers.
- [self checkIfAllDrunk];
- }else{
- [self cantAddMoreThanDrunk];
- }
- }
- -(void)checkIfAllDrunk{
- if (drunkByPersonOne + drunkByPersonTwo == totalBeersToBeDrunk){
- NSLog(@"All beers drunk");
- [self finishedBeers];
- }
- }
- -(void)finishedBeers{
- //Alerts the user that they have finished and how many each person has had.
- NSString * errorString = [NSString stringWithFormat:@"You Have finished all your beers :/ \n %@ had %d \n %@ had %d" , personOneName.text, drunkByPersonOne, personTwoName.text, drunkByPersonTwo];
- UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:nil message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [errorAlert show];
- }
- -(void)cantAddMoreThanDrunk{
- //alerts the user that they have finished and can not add anymore beers.
- NSString * errorString = [NSString stringWithFormat:@"You Have finished all your beers. \n You cant add anymore."];
- UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:nil message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [errorAlert show];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement