diff --git a/ContaPessoas.xcodeproj/project.pbxproj b/ContaPessoas.xcodeproj/project.pbxproj index 9ce30d1..8d46f1d 100644 --- a/ContaPessoas.xcodeproj/project.pbxproj +++ b/ContaPessoas.xcodeproj/project.pbxproj @@ -105,8 +105,8 @@ 27A199B31A94B2A8008DC684 /* Images.xcassets */, 27A199B51A94B2A8008DC684 /* LaunchScreen.xib */, 27A199A31A94B2A8008DC684 /* Supporting Files */, - 27A199D21A94D1FA008DC684 /* Contador.m */, 27A199D41A94D22B008DC684 /* Contador.h */, + 27A199D21A94D1FA008DC684 /* Contador.m */, ); path = ContaPessoas; sourceTree = ""; @@ -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/Base.lproj/Main.storyboard b/ContaPessoas/Base.lproj/Main.storyboard index 99fa0dc..95e81ba 100644 --- a/ContaPessoas/Base.lproj/Main.storyboard +++ b/ContaPessoas/Base.lproj/Main.storyboard @@ -2,6 +2,7 @@ + @@ -13,21 +14,25 @@ - + - - - - - - - - - - - - - - - - - - - + @@ -162,7 +148,7 @@ - + diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..d7aaabd 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,13 +6,27 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // -@interface Contador : NSObject +#import + +@protocol AtualizarValores + +@required +-(void)AtualizarValores; + +@end + +@interface Contador : NSObject + +@property(nonatomic,assign) id mostrarValores; + ++(Contador *)globalContador; - (void)maisUmCueca; - (void)maisUmaGata; -(int)getBoys; -(int)getGirls; +-(int)getTotal; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..63d3012 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -9,12 +9,21 @@ #import #import "Contador.h" - @implementation Contador { int boy; int girl; } +static Contador *globalContador = nil; + ++(Contador *)globalContador; +{ + if (!globalContador) { + globalContador = [[Contador alloc]init]; + } + return globalContador; +} + -(id)init { self = [super init]; if (self) { @@ -26,9 +35,11 @@ -(id)init { - (void)maisUmCueca { boy = boy + 1; + [_mostrarValores AtualizarValores]; } - (void)maisUmaGata { girl++; + [_mostrarValores AtualizarValores]; } -(int)getBoys { @@ -39,7 +50,9 @@ -(int)getGirls { return girl; } - +-(int)getTotal{ + return boy + girl; +} @end diff --git a/ContaPessoas/FirstViewController.h b/ContaPessoas/FirstViewController.h index 59c8301..4322065 100644 --- a/ContaPessoas/FirstViewController.h +++ b/ContaPessoas/FirstViewController.h @@ -7,8 +7,11 @@ // #import +#import "Contador.h" -@interface FirstViewController : UIViewController +@interface FirstViewController : UIViewController{ + Contador *globalContador; +} - (IBAction)clickBoy:(id)sender; - (IBAction)clickGirl:(id)sender; diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..5a2ab9a 100644 --- a/ContaPessoas/FirstViewController.m +++ b/ContaPessoas/FirstViewController.m @@ -7,7 +7,7 @@ // #import "FirstViewController.h" -#import "Contador.h" +#import "SecondViewController.h" @interface FirstViewController () @@ -19,7 +19,7 @@ @implementation FirstViewController { - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador globalContador]; // Do any additional setup after loading the view, typically from a nib. } diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..a7b3361 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,12 +7,17 @@ // #import +#import "Contador.h" -@interface SecondViewController : UIViewController +@interface SecondViewController : UIViewController +{ + Contador *globalContador; +} @property (weak, nonatomic) IBOutlet UILabel *totalBoys; @property (weak, nonatomic) IBOutlet UILabel *totalGirls; @property (weak, nonatomic) IBOutlet UILabel *total; + - (IBAction)click:(id)sender; @end diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..4dd9ba6 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -7,33 +7,36 @@ // #import "SecondViewController.h" +#import "FirstViewController.h" #import "Contador.h" -@interface SecondViewController () { - Contador *contador; -} - -@end - @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + // self.mostrarValores; + [self AtualizarValores]; } - - - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)click:(id)sender { + Contador *contador = [Contador globalContador]; + _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]]; } +- (void)AtualizarValores{ + Contador *contador = [Contador globalContador]; + + _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal]]; +} @end diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..276849d 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -16,11 +16,11 @@ @interface ContaPessoasTests : XCTestCase @implementation ContaPessoasTests - - (void)testContaMeninos { - Contador *c = [[Contador alloc] init]; - [c maisUmCueca]; - XCTAssert(([c getBoys] == 1), @"Pass"); + Contador *c = [[Contador alloc] init]; // Cria um contatdor e aloca na memória + [c maisUmCueca]; // Cria um método + XCTAssert(([c getBoys] == 1), @"Pass"); // + XCTAssert(([c getGirls] == 0), @"Pass"); // } - (void)testContaMeninas { @@ -30,5 +30,12 @@ - (void)testContaMeninas { XCTAssert(([c getBoys] == 0), @"Pass"); } +- (void)testContaTotal { + Contador *c = [[Contador alloc] init]; + [c maisUmaGata]; + XCTAssert(([c getGirls] == 1), @"Pass"); + XCTAssert(([c getBoys] == 1), @"Pass"); + XCTAssert(([c getTotal] == 1), @"Pass"); +} @end