diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..c31ee69 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -5,14 +5,25 @@ // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +#import + + +@protocol Mostrador + +-(void) atualiza; + +@end @interface Contador : NSObject ++ (Contador *) contador; - (void)maisUmCueca; - (void)maisUmaGata; +@property (nonatomic,weak) id delegate; + -(int)getBoys; -(int)getGirls; - +-(int)getTotal; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..00d9d43 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -26,9 +26,11 @@ -(id)init { - (void)maisUmCueca { boy = boy + 1; + [_delegate atualiza]; } - (void)maisUmaGata { girl++; + [_delegate atualiza]; } -(int)getBoys { @@ -39,6 +41,20 @@ -(int)getGirls { return girl; } +static Contador *contador = nil; + ++ (Contador *) contador { + + if (contador == nil) { + contador = [[Contador alloc]init]; + } + return contador; +} + +-(int)getTotal{ + return boy + girl; + +} @end diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..1c538f5 100644 --- a/ContaPessoas/FirstViewController.m +++ b/ContaPessoas/FirstViewController.m @@ -19,7 +19,7 @@ @implementation FirstViewController { - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador contador]; // Do any additional setup after loading the view, typically from a nib. } diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..7bf9e08 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -5,15 +5,16 @@ // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // - +#import "Contador.h" #import -@interface SecondViewController : UIViewController +@interface SecondViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *totalBoys; @property (weak, nonatomic) IBOutlet UILabel *totalGirls; @property (weak, nonatomic) IBOutlet UILabel *total; -- (IBAction)click:(id)sender; +- (IBAction)click:(id)sender; // oi? + @end diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..8f96fab 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -19,9 +19,14 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador contador]; + contador.delegate = self; + [self atualiza]; } +// -(void) viewDidAppear:(BOOL)animated{ +// [self atualiza]; +// } - (void)didReceiveMemoryWarning { @@ -29,10 +34,16 @@ - (void)didReceiveMemoryWarning { // Dispose of any resources that can be recreated. } +-(void)atualiza{ + _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal]]; +} + - (IBAction)click:(id)sender { _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..68903b3 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -21,6 +21,8 @@ - (void)testContaMeninos { Contador *c = [[Contador alloc] init]; [c maisUmCueca]; XCTAssert(([c getBoys] == 1), @"Pass"); + XCTAssert(([c getGirls] == 0), @"Pass"); + XCTAssert(([c getTotal] == 1), @"Pass"); } - (void)testContaMeninas { @@ -28,7 +30,18 @@ - (void)testContaMeninas { [c maisUmaGata]; XCTAssert(([c getGirls] == 1), @"Pass"); XCTAssert(([c getBoys] == 0), @"Pass"); + XCTAssert(([c getTotal] == 1), @"Pass"); } +- (void)testContaTotal { + Contador *c = [[Contador alloc] init]; + [c maisUmaGata]; + [c maisUmCueca]; + [c maisUmCueca]; + [c maisUmCueca]; + XCTAssert(([c getGirls] == 1), @"Pass"); + XCTAssert(([c getBoys] == 3), @"Pass"); + XCTAssert(([c getTotal] == 4), @"Pass"); +} @end