From b9e641c12dfe0dfbfb7cf6f6d8a7840621ca8ee0 Mon Sep 17 00:00:00 2001 From: Bruno Omella Mainieri Date: Thu, 19 Feb 2015 14:19:02 -0200 Subject: [PATCH 1/5] Teste com comentario apenas --- ContaPessoas/SecondViewController.m | 1 + 1 file changed, 1 insertion(+) diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..ee027eb 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -5,6 +5,7 @@ // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +// TESTE DO GIT, COMENTANDO APENAS #import "SecondViewController.h" #import "Contador.h" From 9a88ecd710894c02eeab3b5ecb098519bb87c90b Mon Sep 17 00:00:00 2001 From: Bruno Omella Mainieri Date: Thu, 19 Feb 2015 15:51:00 -0200 Subject: [PATCH 2/5] =?UTF-8?q?Aplicado=20padr=C3=A3o=20Singleton=20ao=20C?= =?UTF-8?q?ontador;=20atualizados=20testes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ContaPessoas/Contador.h | 3 +++ ContaPessoas/Contador.m | 11 +++++++++++ ContaPessoas/FirstViewController.m | 2 +- ContaPessoas/SecondViewController.m | 2 +- ContaPessoasTests/ContaPessoasTests.m | 14 ++++++++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..811c868 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -8,11 +8,14 @@ @interface Contador : NSObject ++(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..fcf71df 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -15,6 +15,8 @@ @implementation Contador { int girl; } +static Contador *_instancia; + -(id)init { self = [super init]; if (self) { @@ -24,6 +26,12 @@ -(id)init { return self; } ++(Contador *)instancia{ + if(_instancia == nil) + _instancia = [[Contador alloc]init]; + return _instancia; +} + - (void)maisUmCueca { boy = boy + 1; } @@ -39,6 +47,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.m b/ContaPessoas/SecondViewController.m index ee027eb..9329837 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -20,7 +20,7 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador instancia]; } 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"); } From b7a2f6b50704cdf194f3af23e4d192835821d9f6 Mon Sep 17 00:00:00 2001 From: Bruno Omella Mainieri Date: Thu, 19 Feb 2015 17:23:26 -0200 Subject: [PATCH 3/5] Implementado Notification --- ContaPessoas/Contador.m | 7 +++++++ ContaPessoas/SecondViewController.m | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fcf71df..2692112 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -22,6 +22,7 @@ -(id)init { if (self) { boy = 0; girl = 0; + } return self; } @@ -34,9 +35,15 @@ +(Contador *)instancia{ - (void)maisUmCueca { boy = boy + 1; + [[NSNotificationCenter defaultCenter] postNotificationName:@"NumUpdate" + object:self + userInfo:@{@"boy":[NSNumber numberWithInt: boy], @"girl":[NSNumber numberWithInt:girl],@"total":[NSNumber numberWithInt: boy+girl]}]; } - (void)maisUmaGata { girl++; + [[NSNotificationCenter defaultCenter] postNotificationName:@"NumUpdate" + object:self + userInfo:@{@"boy":[NSNumber numberWithInt: boy], @"girl":[NSNumber numberWithInt:girl],@"total":[NSNumber numberWithInt: boy+girl]}]; } -(int)getBoys { diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index 9329837..809ee48 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -5,7 +5,6 @@ // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // -// TESTE DO GIT, COMENTANDO APENAS #import "SecondViewController.h" #import "Contador.h" @@ -21,9 +20,15 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; contador = [Contador instancia]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:@"NumUpdate" object:nil]; } - +//atualização dos dados quando o contador é alterado +-(void)update:(NSNotification*)notification{ + _totalBoys.text = [NSString stringWithFormat: @"%d", [notification.userInfo[@"boy"] intValue]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [notification.userInfo[@"girl"] intValue]]; + _total.text = [NSString stringWithFormat: @"%d", [notification.userInfo[@"total"] intValue]]; +} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; From 66514418dedb6db8c6a40f973336d33bbc76cda0 Mon Sep 17 00:00:00 2001 From: Bruno Omella Mainieri Date: Fri, 20 Feb 2015 14:59:08 -0200 Subject: [PATCH 4/5] Subsittuido Notification por Delegate --- ContaPessoas.xcodeproj/project.pbxproj | 2 ++ ContaPessoas/Contador.h | 17 ++++++++++++++++- ContaPessoas/Contador.m | 16 ++++++++++------ ContaPessoas/SecondViewController.h | 3 ++- ContaPessoas/SecondViewController.m | 21 +++++++++++++-------- 5 files changed, 43 insertions(+), 16 deletions(-) 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 811c868..69ab85b 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,7 +6,22 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // -@interface Contador : NSObject +@protocol MostradorDelegate +@required +-(void)showBoys:(int)b; +-(void)showGirls:(int)g; +-(void)showTotal:(int)t; + +@end + +@interface Contador : NSObject +{ + id delegate; +} + +@property (retain) id delegate; + +- (void) setDelegate:(id)newDelegate; +(Contador *)instancia; diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index 2692112..8dea980 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -15,6 +15,12 @@ @implementation Contador { int girl; } +@synthesize delegate; + +- (void) setDelegate:(id)newDelegate{ + delegate = newDelegate; +} + static Contador *_instancia; -(id)init { @@ -35,15 +41,13 @@ +(Contador *)instancia{ - (void)maisUmCueca { boy = boy + 1; - [[NSNotificationCenter defaultCenter] postNotificationName:@"NumUpdate" - object:self - userInfo:@{@"boy":[NSNumber numberWithInt: boy], @"girl":[NSNumber numberWithInt:girl],@"total":[NSNumber numberWithInt: boy+girl]}]; + [delegate showBoys:boy]; + [[self delegate]showTotal:boy+girl]; } - (void)maisUmaGata { girl++; - [[NSNotificationCenter defaultCenter] postNotificationName:@"NumUpdate" - object:self - userInfo:@{@"boy":[NSNumber numberWithInt: boy], @"girl":[NSNumber numberWithInt:girl],@"total":[NSNumber numberWithInt: boy+girl]}]; + [delegate showGirls:girl]; + [[self delegate]showTotal:boy+girl]; } -(int)getBoys { 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 809ee48..be36d31 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -7,7 +7,6 @@ // #import "SecondViewController.h" -#import "Contador.h" @interface SecondViewController () { Contador *contador; @@ -17,18 +16,24 @@ @interface SecondViewController () { @implementation SecondViewController +-(void)showBoys:(int)b{ + _totalBoys.text = [NSString stringWithFormat: @"%d", b]; +} +-(void)showGirls:(int)g{ + _totalGirls.text = [NSString stringWithFormat: @"%d", g]; +} +-(void)showTotal:(int)t{ + _total.text = [NSString stringWithFormat:@"%d", t]; +} + - (void)viewDidLoad { [super viewDidLoad]; contador = [Contador instancia]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:@"NumUpdate" object:nil]; + [self click:self]; + [contador setDelegate:self]; + } -//atualização dos dados quando o contador é alterado --(void)update:(NSNotification*)notification{ - _totalBoys.text = [NSString stringWithFormat: @"%d", [notification.userInfo[@"boy"] intValue]]; - _totalGirls.text = [NSString stringWithFormat: @"%d", [notification.userInfo[@"girl"] intValue]]; - _total.text = [NSString stringWithFormat: @"%d", [notification.userInfo[@"total"] intValue]]; -} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; From dfacd4c782f09be80ece48b7b3822b105586fe55 Mon Sep 17 00:00:00 2001 From: Bruno Omella Mainieri Date: Fri, 20 Feb 2015 15:07:00 -0200 Subject: [PATCH 5/5] Melhorado delegate. --- ContaPessoas/Contador.h | 5 +---- ContaPessoas/Contador.m | 6 ++---- ContaPessoas/SecondViewController.m | 12 ++++-------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 69ab85b..dad92fa 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -8,15 +8,12 @@ @protocol MostradorDelegate @required --(void)showBoys:(int)b; --(void)showGirls:(int)g; --(void)showTotal:(int)t; +-(void)mostraDados; @end @interface Contador : NSObject { - id delegate; } @property (retain) id delegate; diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index 8dea980..6ba3a9c 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -41,13 +41,11 @@ +(Contador *)instancia{ - (void)maisUmCueca { boy = boy + 1; - [delegate showBoys:boy]; - [[self delegate]showTotal:boy+girl]; + [delegate mostraDados]; } - (void)maisUmaGata { girl++; - [delegate showGirls:girl]; - [[self delegate]showTotal:boy+girl]; + [delegate mostraDados]; } -(int)getBoys { diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index be36d31..62ff394 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -16,14 +16,10 @@ @interface SecondViewController () { @implementation SecondViewController --(void)showBoys:(int)b{ - _totalBoys.text = [NSString stringWithFormat: @"%d", b]; -} --(void)showGirls:(int)g{ - _totalGirls.text = [NSString stringWithFormat: @"%d", g]; -} --(void)showTotal:(int)t{ - _total.text = [NSString stringWithFormat:@"%d", t]; +-(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 {