From 2a2d32def6f73a6aff789f95800ccd9b70d8bc8e Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Thu, 19 Feb 2015 14:23:13 -0200 Subject: [PATCH 1/7] Comentario --- ContaPessoas/AppDelegate.h | 2 ++ ContaPessoas/Contador.m | 1 + 2 files changed, 3 insertions(+) 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.m b/ContaPessoas/Contador.m index fa64ad8..e46a939 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -5,6 +5,7 @@ // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +//Patricia Abreu #import #import "Contador.h" From 3af4b4384fe20c353ffe1fc414755ac7361e853a Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Thu, 19 Feb 2015 15:38:53 -0200 Subject: [PATCH 2/7] =?UTF-8?q?uso=20do=20padr=C3=A3o=20Singleton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ContaPessoas/Contador.h | 1 + ContaPessoas/Contador.m | 9 +++++++++ ContaPessoas/FirstViewController.m | 2 +- ContaPessoas/SecondViewController.m | 3 ++- ContaPessoasTests/ContaPessoasTests.m | 3 +++ 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..5d2933f 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -8,6 +8,7 @@ @interface Contador : NSObject ++(Contador *) contador; - (void)maisUmCueca; - (void)maisUmaGata; diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index e46a939..e3b6dba 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -16,6 +16,15 @@ @implementation Contador { int girl; } + static Contador *contador = nil; + ++(Contador *) contador{ + if(contador == nil){ + contador = [[Contador alloc] init]; + } + return contador; +} + -(id)init { self = [super init]; if (self) { 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.m b/ContaPessoas/SecondViewController.m index a2a66f5..5fd6479 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -19,7 +19,7 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador contador]; } @@ -33,6 +33,7 @@ - (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] ]; + } diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..c4d7412 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 { From 4d503601552f17350b1ec4b3b717e3b132ab486b Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Thu, 19 Feb 2015 15:53:32 -0200 Subject: [PATCH 3/7] getTotal Implementado --- ContaPessoas/Contador.h | 1 + ContaPessoas/Contador.m | 3 +++ ContaPessoas/SecondViewController.m | 2 +- ContaPessoasTests/ContaPessoasTests.m | 7 +++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 5d2933f..23380a5 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -14,6 +14,7 @@ -(int)getBoys; -(int)getGirls; +-(int)getTotal; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index e3b6dba..92bdd6b 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -49,6 +49,9 @@ -(int)getGirls { return girl; } +-(int)getTotal{ + return boy + girl; +} @end diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index 5fd6479..27d124f 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -32,7 +32,7 @@ - (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] ]; } diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index c4d7412..263d26c 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -33,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 From 942fb9df124b3add6411047240c8c1a42c34e401 Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Fri, 20 Feb 2015 15:17:53 -0200 Subject: [PATCH 4/7] implementado o Delegate --- ContaPessoas.xcodeproj/project.pbxproj | 4 ++++ ContaPessoas/Contador.h | 5 +++++ ContaPessoas/Contador.m | 19 +++++++++++++++++-- ContaPessoas/Mostrador.h | 16 ++++++++++++++++ ContaPessoas/Mostrador.m | 13 +++++++++++++ ContaPessoas/SecondViewController.h | 3 ++- ContaPessoas/SecondViewController.m | 13 +++++++++++++ 7 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 ContaPessoas/Mostrador.h create mode 100644 ContaPessoas/Mostrador.m 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/Contador.h b/ContaPessoas/Contador.h index 23380a5..9bb4ec4 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,8 +6,13 @@ // 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; diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index 92bdd6b..6ec83e8 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -10,11 +10,11 @@ #import #import "Contador.h" - @implementation Contador { int boy; int girl; } +@synthesize mostradorDelegate; static Contador *contador = nil; @@ -25,6 +25,19 @@ +(Contador *) contador{ 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) { @@ -35,10 +48,12 @@ -(id)init { } - (void)maisUmCueca { - boy = boy + 1; + boy ++; + [mostradorDelegate atualiza]; } - (void)maisUmaGata { girl++; + [mostradorDelegate atualiza]; } -(int)getBoys { diff --git a/ContaPessoas/Mostrador.h b/ContaPessoas/Mostrador.h new file mode 100644 index 0000000..9f2ea1f --- /dev/null +++ b/ContaPessoas/Mostrador.h @@ -0,0 +1,16 @@ +// +// Mostrador.h +// ContaPessoas +// +// Created by Patricia Machado de Abreu on 20/02/15. +// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// + +#import + +@protocol MostradorDelegate + +-(void)atualiza; + +@end + diff --git a/ContaPessoas/Mostrador.m b/ContaPessoas/Mostrador.m new file mode 100644 index 0000000..fa878dc --- /dev/null +++ b/ContaPessoas/Mostrador.m @@ -0,0 +1,13 @@ +// +// Mostrador.m +// ContaPessoas +// +// Created by Patricia Machado de Abreu on 20/02/15. +// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// + +#import "Mostrador.h" + +@implementation Mostrador + +@end diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..e543e61 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,8 +7,9 @@ // #import +#import "Mostrador.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 27d124f..dace0f2 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; @@ -20,8 +21,14 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; contador = [Contador contador]; + contador.mostradorDelegate = self; + [self atualiza]; } +//Não é delegate mas soluciona o problema +//-(void) viewDidAppear:(BOOL)animated{ +// [self click:self]; +//} - (void)didReceiveMemoryWarning { @@ -36,5 +43,11 @@ - (IBAction)click:(id)sender { } +-(void)atualiza{ + _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal] ]; +} + @end From 48ce1b1f5bb6d7ec244c578e757ce041cf73ae7a Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Fri, 20 Feb 2015 15:47:32 -0200 Subject: [PATCH 5/7] comentarios no codigo sobre delegate --- ContaPessoas/Contador.m | 3 +++ ContaPessoas/SecondViewController.m | 2 ++ 2 files changed, 5 insertions(+) diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index 6ec83e8..09b5d93 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -14,6 +14,7 @@ @implementation Contador { int boy; int girl; } + @synthesize mostradorDelegate; static Contador *contador = nil; @@ -49,10 +50,12 @@ -(id)init { - (void)maisUmCueca { boy ++; +// informa o delegate que os dados foram atualizados [mostradorDelegate atualiza]; } - (void)maisUmaGata { girl++; +// informa o delegate que os dados foram atualizados [mostradorDelegate atualiza]; } diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index dace0f2..17f591f 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -22,6 +22,7 @@ - (void)viewDidLoad { [super viewDidLoad]; contador = [Contador contador]; contador.mostradorDelegate = self; +// faz atualizar as informaçoes do second view na primeira vez que entra nele [self atualiza]; } @@ -43,6 +44,7 @@ - (IBAction)click:(id)sender { } +//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]]; From f6d9f00840f9a6d3dd23eaba4c434ce3d7f010e5 Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Fri, 20 Feb 2015 15:52:15 -0200 Subject: [PATCH 6/7] =?UTF-8?q?comentarios=20no=20c=C3=B3digo=20sobre=20o?= =?UTF-8?q?=20uso=20do=20padr=C3=A3o=20delegate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ContaPessoas/Mostrador.h | 1 + ContaPessoas/SecondViewController.h | 1 + 2 files changed, 2 insertions(+) diff --git a/ContaPessoas/Mostrador.h b/ContaPessoas/Mostrador.h index 9f2ea1f..b19538a 100644 --- a/ContaPessoas/Mostrador.h +++ b/ContaPessoas/Mostrador.h @@ -8,6 +8,7 @@ #import +//interface @protocol MostradorDelegate -(void)atualiza; diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index e543e61..89df5f0 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -9,6 +9,7 @@ #import #import "Mostrador.h" +//o esteriotipo informa que o SecondViewController implementa a interface MostradorDelegate @interface SecondViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *totalBoys; From 8ef5a10e53af925906fd9a04febf61b62bb43897 Mon Sep 17 00:00:00 2001 From: Patricia Machado de Abreu Date: Fri, 20 Feb 2015 15:55:07 -0200 Subject: [PATCH 7/7] classe Mostrador.m apagado --- ContaPessoas/Mostrador.m | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 ContaPessoas/Mostrador.m diff --git a/ContaPessoas/Mostrador.m b/ContaPessoas/Mostrador.m deleted file mode 100644 index fa878dc..0000000 --- a/ContaPessoas/Mostrador.m +++ /dev/null @@ -1,13 +0,0 @@ -// -// Mostrador.m -// ContaPessoas -// -// Created by Patricia Machado de Abreu on 20/02/15. -// Copyright (c) 2015 Vinicius Miana. All rights reserved. -// - -#import "Mostrador.h" - -@implementation Mostrador - -@end