diff --git a/ContaPessoas.xcodeproj/project.pbxproj b/ContaPessoas.xcodeproj/project.pbxproj index 9ce30d1..9efbdb5 100644 --- a/ContaPessoas.xcodeproj/project.pbxproj +++ b/ContaPessoas.xcodeproj/project.pbxproj @@ -52,6 +52,7 @@ 27A199D21A94D1FA008DC684 /* Contador.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Contador.m; sourceTree = ""; }; 27A199D41A94D22B008DC684 /* Contador.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Contador.h; sourceTree = ""; }; 27A199D51A94D821008DC684 /* ContadorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ContadorTests.m; sourceTree = ""; }; + 4D4EDB891A97934400145B42 /* Mostrador.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mostrador.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -106,6 +107,7 @@ 27A199B51A94B2A8008DC684 /* LaunchScreen.xib */, 27A199A31A94B2A8008DC684 /* Supporting Files */, 27A199D21A94D1FA008DC684 /* Contador.m */, + 4D4EDB891A97934400145B42 /* Mostrador.h */, 27A199D41A94D22B008DC684 /* Contador.h */, ); path = ContaPessoas; @@ -438,6 +440,7 @@ 27A199C81A94B2A8008DC684 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 27A199C91A94B2A8008DC684 /* Build configuration list for PBXNativeTarget "ContaPessoasTests" */ = { isa = XCConfigurationList; @@ -446,6 +449,7 @@ 27A199CB1A94B2A8008DC684 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ContaPessoas/AppDelegate.h b/ContaPessoas/AppDelegate.h index a4e16ef..be1f526 100644 --- a/ContaPessoas/AppDelegate.h +++ b/ContaPessoas/AppDelegate.h @@ -6,6 +6,8 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // + + #import @interface AppDelegate : UIResponder diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..9bb4ec4 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,13 +6,20 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +#import "Mostrador.h" + @interface Contador : NSObject +//Associa com o Delegate +@property id mostradorDelegate; + ++(Contador *) contador; - (void)maisUmCueca; - (void)maisUmaGata; -(int)getBoys; -(int)getGirls; +-(int)getTotal; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..09b5d93 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -5,16 +5,40 @@ // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +//Patricia Abreu #import #import "Contador.h" - @implementation Contador { int boy; int girl; } +@synthesize mostradorDelegate; + + static Contador *contador = nil; + ++(Contador *) contador{ + if(contador == nil){ + contador = [[Contador alloc] init]; + } + return contador; +} + + +//Outro modo de fazer o Singleton + +//(Contador *) instance{ +//static Contador *sharedContador = nil; +//static dispatch_once_t onceToken; +//dispatch_once(&onceToken, +// { +// sharedContador = [[self alloc] init]; +// }); +//return sharedContador; +//} + -(id)init { self = [super init]; if (self) { @@ -25,10 +49,14 @@ -(id)init { } - (void)maisUmCueca { - boy = boy + 1; + boy ++; +// informa o delegate que os dados foram atualizados + [mostradorDelegate atualiza]; } - (void)maisUmaGata { girl++; +// informa o delegate que os dados foram atualizados + [mostradorDelegate atualiza]; } -(int)getBoys { @@ -39,6 +67,9 @@ -(int)getGirls { return girl; } +-(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/Mostrador.h b/ContaPessoas/Mostrador.h new file mode 100644 index 0000000..b19538a --- /dev/null +++ b/ContaPessoas/Mostrador.h @@ -0,0 +1,17 @@ +// +// Mostrador.h +// ContaPessoas +// +// Created by Patricia Machado de Abreu on 20/02/15. +// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// + +#import + +//interface +@protocol MostradorDelegate + +-(void)atualiza; + +@end + diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..89df5f0 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,8 +7,10 @@ // #import +#import "Mostrador.h" -@interface SecondViewController : UIViewController +//o esteriotipo informa que o SecondViewController implementa a interface MostradorDelegate +@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..17f591f 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -8,6 +8,7 @@ #import "SecondViewController.h" #import "Contador.h" +#import "Mostrador.h" @interface SecondViewController () { Contador *contador; @@ -19,9 +20,16 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador contador]; + contador.mostradorDelegate = self; +// faz atualizar as informaçoes do second view na primeira vez que entra nele + [self atualiza]; } +//Não é delegate mas soluciona o problema +//-(void) viewDidAppear:(BOOL)animated{ +// [self click:self]; +//} - (void)didReceiveMemoryWarning { @@ -32,7 +40,15 @@ - (void)didReceiveMemoryWarning { - (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] ]; + +} + +//implementa o metodo declarado no MostradorDelegate, pois é uma interface e o second view implementa essa classe +-(void)atualiza{ + _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal] ]; } diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..263d26c 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -21,6 +21,9 @@ - (void)testContaMeninos { Contador *c = [[Contador alloc] init]; [c maisUmCueca]; XCTAssert(([c getBoys] == 1), @"Pass"); +// teste de meninas +// XCTAssert(([c getGirls] == 1), @"Pass"); + XCTAssert(([c getGirls] == 0), @"Pass"); } - (void)testContaMeninas { @@ -30,5 +33,12 @@ - (void)testContaMeninas { XCTAssert(([c getBoys] == 0), @"Pass"); } +-(void)testContaTotal{ + Contador *c = [[Contador alloc] init]; + [c maisUmaGata]; + [c maisUmaGata]; + [c maisUmCueca]; + XCTAssert(([c getTotal] == 3), @"Pass"); +} @end