diff --git a/ContaPessoas/Base.lproj/Main.storyboard b/ContaPessoas/Base.lproj/Main.storyboard index 99fa0dc..d28dee7 100644 --- a/ContaPessoas/Base.lproj/Main.storyboard +++ b/ContaPessoas/Base.lproj/Main.storyboard @@ -2,6 +2,7 @@ + @@ -13,21 +14,14 @@ - + - - - + - - - - - - - - - + + + + + + + + + @@ -123,15 +128,6 @@ - diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..8bfd599 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -8,11 +8,17 @@ @interface Contador : NSObject ++ (Contador *) contGlobal; + - (void)maisUmCueca; - (void)maisUmaGata; -(int)getBoys; -(int)getGirls; +-(int)getTotal; + +@property (nonatomic, weak) id mostrar; + @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..f198b38 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -8,6 +8,7 @@ #import #import "Contador.h" +#import "SecondViewController.h" @implementation Contador { @@ -15,6 +16,15 @@ @implementation Contador { int girl; } ++ (Contador *)contGlobal{ + static Contador *contGlobal = nil; + + if (!contGlobal){ + contGlobal = [[Contador alloc] init]; + } + return contGlobal; +} + -(id)init { self = [super init]; if (self) { @@ -26,9 +36,11 @@ -(id)init { - (void)maisUmCueca { boy = boy + 1; + [_mostrar atualiza]; } - (void)maisUmaGata { girl++; + [_mostrar atualiza]; } -(int)getBoys { @@ -39,6 +51,10 @@ -(int)getGirls { return girl; } +-(int)getTotal{ + return boy + girl; +} + @end diff --git a/ContaPessoas/FirstViewController.h b/ContaPessoas/FirstViewController.h index 59c8301..cf4a501 100644 --- a/ContaPessoas/FirstViewController.h +++ b/ContaPessoas/FirstViewController.h @@ -7,8 +7,11 @@ // #import +@class Contador; -@interface FirstViewController : UIViewController +@interface FirstViewController : UIViewController{ + Contador *contador; +} - (IBAction)clickBoy:(id)sender; - (IBAction)clickGirl:(id)sender; diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..a8d21eb 100644 --- a/ContaPessoas/FirstViewController.m +++ b/ContaPessoas/FirstViewController.m @@ -2,24 +2,26 @@ // FirstViewController.m // ContaPessoas // -// Created by Vinicius Miana on 2/18/15. -// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// Created by Lidia Chou on 2/18/15. +// Copyright (c) 2015 Lidia Chou. All rights reserved. +// +// // #import "FirstViewController.h" #import "Contador.h" +#import "SecondViewController.h" @interface FirstViewController () @end -@implementation FirstViewController { - Contador *contador; -} +@implementation FirstViewController + - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador contGlobal]; // Do any additional setup after loading the view, typically from a nib. } diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..d5d8aca 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,13 +7,17 @@ // #import +@class Contador; -@interface SecondViewController : UIViewController +@interface SecondViewController : UIViewController{ + Contador *contador; +} @property (weak, nonatomic) IBOutlet UILabel *totalBoys; @property (weak, nonatomic) IBOutlet UILabel *totalGirls; @property (weak, nonatomic) IBOutlet UILabel *total; -- (IBAction)click:(id)sender; + +-(void)atualiza; @end diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..c4ef08e 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -8,10 +8,10 @@ #import "SecondViewController.h" #import "Contador.h" +#import "FirstViewController.h" + +@interface SecondViewController () -@interface SecondViewController () { - Contador *contador; -} @end @@ -19,7 +19,9 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador contGlobal]; + contador.mostrar = self; + [self atualiza]; } @@ -29,10 +31,10 @@ - (void)didReceiveMemoryWarning { // Dispose of any resources that can be recreated. } -- (IBAction)click:(id)sender { +- (void)atualiza { _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; - _total.text = [NSString stringWithFormat:@"%d", [contador getGirls] + [contador getBoys] ]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal] ]; } diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..8a36f2f 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -21,6 +21,7 @@ - (void)testContaMeninos { Contador *c = [[Contador alloc] init]; [c maisUmCueca]; XCTAssert(([c getBoys] == 1), @"Pass"); + XCTAssert(([c getGirls] == 0), @"Pass"); } - (void)testContaMeninas {