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/AppDelegate.h b/ContaPessoas/AppDelegate.h index a4e16ef..81895ab 100644 --- a/ContaPessoas/AppDelegate.h +++ b/ContaPessoas/AppDelegate.h @@ -14,4 +14,3 @@ @end - diff --git a/ContaPessoas/Base.lproj/Main.storyboard b/ContaPessoas/Base.lproj/Main.storyboard index 99fa0dc..56cc345 100644 --- a/ContaPessoas/Base.lproj/Main.storyboard +++ b/ContaPessoas/Base.lproj/Main.storyboard @@ -1,193 +1,184 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..3f57718 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,13 +6,22 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +@protocol Mostrador + +@required +-(void)atualiza; + +@end + @interface Contador : NSObject ++ (Contador *)getInstance; + - (void)maisUmCueca; - (void)maisUmaGata; --(int)getBoys; --(int)getGirls; +@property (nonatomic, weak) id mostrar; +@property int boy; +@property int girl; @end - diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..ae39e41 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -9,37 +9,36 @@ #import #import "Contador.h" +@implementation Contador -@implementation Contador { - int boy; - int girl; +#pragma mark Singleton + ++ (Contador *)getInstance { + static Contador *contadorCompartilhado = nil; + static dispatch_once_t token; + dispatch_once(&token, ^{ + contadorCompartilhado = [[self alloc] init]; + }); + return contadorCompartilhado; } --(id)init { - self = [super init]; - if (self) { - boy = 0; - girl = 0; +- (id)init { + if ((self = [super init])) { + _boy = 0; + _girl = 0; } return self; } +#pragma mark Contador + - (void)maisUmCueca { - boy = boy + 1; + _boy = _boy + 1; + [_mostrar atualiza]; } - (void)maisUmaGata { - girl++; + _girl++; + [_mostrar atualiza]; } --(int)getBoys { - return boy; -} - --(int)getGirls { - return girl; -} - - - @end - diff --git a/ContaPessoas/FirstViewController.h b/ContaPessoas/FirstViewController.h index 59c8301..3895b33 100644 --- a/ContaPessoas/FirstViewController.h +++ b/ContaPessoas/FirstViewController.h @@ -7,11 +7,13 @@ // #import +#import "Contador.h" -@interface FirstViewController : UIViewController +@interface FirstViewController : UIViewController { + Contador *contador; +} - (IBAction)clickBoy:(id)sender; - (IBAction)clickGirl:(id)sender; @end - diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..30a8db7 100644 --- a/ContaPessoas/FirstViewController.m +++ b/ContaPessoas/FirstViewController.m @@ -2,40 +2,37 @@ // FirstViewController.m // ContaPessoas // -// Created by Vinicius Miana on 2/18/15. +// Created by Eduardo Quadros on 2/18/15. // Copyright (c) 2015 Vinicius Miana. 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]; - // Do any additional setup after loading the view, typically from a nib. + contador = [Contador getInstance]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. } - (IBAction)clickBoy:(id)sender { [contador maisUmCueca]; - NSLog(@"Meninos - %i",[contador getBoys]); + NSLog(@"Meninos - %i", contador.boy); } - (IBAction)clickGirl:(id)sender { [contador maisUmaGata]; - NSLog(@"Meninas - %i",[contador getGirls]); + NSLog(@"Meninas - %i", contador.girl); } @end diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..6e632e9 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,13 +7,16 @@ // #import +#import "Contador.h" -@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; -@end +- (void)atualiza; +@end diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..c8e556b 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -8,10 +8,9 @@ #import "SecondViewController.h" #import "Contador.h" +#import "FirstViewController.h" -@interface SecondViewController () { - Contador *contador; -} +@interface SecondViewController () @end @@ -19,21 +18,19 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador getInstance]; + contador.mostrar = self; + [self atualiza]; } - - - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. } -- (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] ]; +- (void)atualiza { + _totalBoys.text = [NSString stringWithFormat: @"%d", contador.boy]; + _totalGirls.text = [NSString stringWithFormat: @"%d", contador.girl]; + _total.text = [NSString stringWithFormat:@"%d", (contador.boy + contador.girl)]; } - @end diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..442e7f0 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -20,15 +20,24 @@ @implementation ContaPessoasTests - (void)testContaMeninos { Contador *c = [[Contador alloc] init]; [c maisUmCueca]; - XCTAssert(([c getBoys] == 1), @"Pass"); + XCTAssert((c.boy == 1), @"Pass"); } - (void)testContaMeninas { Contador *c = [[Contador alloc] init]; [c maisUmaGata]; - XCTAssert(([c getGirls] == 1), @"Pass"); - XCTAssert(([c getBoys] == 0), @"Pass"); + XCTAssert((c.girl == 1), @"Pass"); + XCTAssert((c.boy == 0), @"Pass"); } +- (void)testContarTodos { + Contador *c = [[Contador alloc] init]; + [c maisUmaGata]; + [c maisUmaGata]; + [c maisUmCueca]; + XCTAssert((c.girl == 2), @"Pass"); + XCTAssert((c.boy == 1), @"Pass"); + XCTAssert((c.girl + c.boy == 3), @"Pass"); +} @end