diff --git a/ContaPessoas.xcodeproj/project.pbxproj b/ContaPessoas.xcodeproj/project.pbxproj index 9ce30d1..ca06d09 100644 --- a/ContaPessoas.xcodeproj/project.pbxproj +++ b/ContaPessoas.xcodeproj/project.pbxproj @@ -438,6 +438,7 @@ 27A199C81A94B2A8008DC684 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 27A199C91A94B2A8008DC684 /* Build configuration list for PBXNativeTarget "ContaPessoasTests" */ = { isa = XCConfigurationList; @@ -446,6 +447,7 @@ 27A199CB1A94B2A8008DC684 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..dad92fa 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,13 +6,28 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // -@interface Contador : NSObject +@protocol MostradorDelegate +@required +-(void)mostraDados; + +@end + +@interface Contador : NSObject +{ +} + +@property (retain) id delegate; + +- (void) setDelegate:(id)newDelegate; + ++(Contador *)instancia; - (void)maisUmCueca; - (void)maisUmaGata; -(int)getBoys; -(int)getGirls; +-(int)getTotal; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..6ba3a9c 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -15,20 +15,37 @@ @implementation Contador { int girl; } +@synthesize delegate; + +- (void) setDelegate:(id)newDelegate{ + delegate = newDelegate; +} + +static Contador *_instancia; + -(id)init { self = [super init]; if (self) { boy = 0; girl = 0; + } return self; } ++(Contador *)instancia{ + if(_instancia == nil) + _instancia = [[Contador alloc]init]; + return _instancia; +} + - (void)maisUmCueca { boy = boy + 1; + [delegate mostraDados]; } - (void)maisUmaGata { girl++; + [delegate mostraDados]; } -(int)getBoys { @@ -39,6 +56,9 @@ -(int)getGirls { return girl; } +-(int)getTotal { + return boy + girl; +} @end diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..128e95a 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 instancia]; // Do any additional setup after loading the view, typically from a nib. } diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..7ed1ce3 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,8 +7,9 @@ // #import +#include "Contador.h" -@interface SecondViewController : UIViewController +@interface SecondViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *totalBoys; @property (weak, nonatomic) IBOutlet UILabel *totalGirls; diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..62ff394 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -7,7 +7,6 @@ // #import "SecondViewController.h" -#import "Contador.h" @interface SecondViewController () { Contador *contador; @@ -17,13 +16,21 @@ @interface SecondViewController () { @implementation SecondViewController +-(void)mostraDados{ + _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; + _total.text = [NSString stringWithFormat: @"%d", [contador getTotal]]; +} + - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador instancia]; + [self click:self]; + [contador setDelegate:self]; + } - - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..9b6baa3 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,6 +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 maisUmaGata]; + [c maisUmCueca]; + XCTAssert(([c getGirls] == 2), @"Pass"); + XCTAssert(([c getBoys] == 2), @"Pass"); + XCTAssert(([c getTotal] == 4), @"Pass"); }