From 0e3c7186a57d3d2117f40a0fa120590ba96fff25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Leal=20Mendon=C3=A7a?= Date: Thu, 19 Feb 2015 14:21:00 -0200 Subject: [PATCH 1/3] =?UTF-8?q?Mudan=C3=A7a=20de=20coment=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Troca de nome no comentário de uma classe --- ContaPessoas/FirstViewController.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ContaPessoas/FirstViewController.h b/ContaPessoas/FirstViewController.h index 59c8301..1785ecf 100644 --- a/ContaPessoas/FirstViewController.h +++ b/ContaPessoas/FirstViewController.h @@ -2,8 +2,8 @@ // FirstViewController.h // ContaPessoas // -// Created by Vinicius Miana on 2/18/15. -// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// Created by Lucas Lea Mendonça on 2/18/15. +// Copyright (c) 2015 Lucas Leal Mendonça. All rights reserved. // #import From 8ea4aa1bb2c25bb231b5b240ef0cb157bf3a8c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Leal=20Mendon=C3=A7a?= Date: Thu, 19 Feb 2015 16:54:57 -0200 Subject: [PATCH 2/3] getTotal, Singleton e "Delegate --- ContaPessoas/Contador.h | 3 ++- ContaPessoas/Contador.m | 14 +++++++++++++- ContaPessoas/FirstViewController.m | 2 +- ContaPessoas/SecondViewController.m | 16 +++++++++++++--- ContaPessoasTests/ContaPessoasTests.m | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..2b01bde 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -11,8 +11,9 @@ - (void)maisUmCueca; - (void)maisUmaGata; ++(Contador *) instancia; -(int)getBoys; -(int)getGirls; - +-(int)getTotal; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..3a3d86a 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -9,12 +9,20 @@ #import #import "Contador.h" - @implementation Contador { int boy; int girl; } +static Contador *_contador = nil; + ++ (Contador *) instancia{ + if (_contador == nil) { + _contador = [[Contador alloc] init]; + } + return _contador; +} + -(id)init { self = [super init]; if (self) { @@ -39,6 +47,10 @@ -(int)getGirls { return girl; } +-(int)getTotal{ + return girl + boy; +} + @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 a2a66f5..4894a6f 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -6,6 +6,14 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // +/*Fazer: + + -> Singleton + -> getTotal + -> teste + -> delegate C (observer) + */ + #import "SecondViewController.h" #import "Contador.h" @@ -19,10 +27,12 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador instancia]; } - +- (void)viewDidAppear:(BOOL)animated{ + [self click:self];//Gambiarra. Please Understand +} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; @@ -32,7 +42,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 579bb3f..f473f8e 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"); + XCTAssert(([c getGirls] == 0), @"Pass"); + XCTAssert(([c getTotal] == 1), @"Pass"); + } - (void)testContaMeninas { @@ -28,6 +31,19 @@ - (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 maisUmCueca]; + XCTAssert(([c getGirls] == 1), @"Pass"); + XCTAssert(([c getBoys] == 2), @"Pass"); + XCTAssert(([c getTotal] == 3), @"Pass"); + } From c23bce5e1265fe38b72c1707130a441a1542b7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Leal=20Mendon=C3=A7a?= Date: Fri, 20 Feb 2015 15:04:57 -0200 Subject: [PATCH 3/3] Delegate legi --- ContaPessoas/Contador.h | 13 ++++++++++++- ContaPessoas/Contador.m | 5 +++++ ContaPessoas/SecondViewController.h | 4 +++- ContaPessoas/SecondViewController.m | 24 +++++++++++++++++++++--- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 2b01bde..5025c94 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,7 +6,18 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // -@interface Contador : NSObject +@protocol ContadorDelegate + +@required +-(void)atualizaDados; + +@end + +@interface Contador : NSObject { + id delegate; +} + +@property(nonatomic, retain) id delegate; - (void)maisUmCueca; - (void)maisUmaGata; diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index 3a3d86a..72d783f 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -14,6 +14,8 @@ @implementation Contador { int girl; } +@synthesize delegate; + static Contador *_contador = nil; + (Contador *) instancia{ @@ -26,6 +28,7 @@ + (Contador *) instancia{ -(id)init { self = [super init]; if (self) { + delegate = nil; boy = 0; girl = 0; } @@ -34,9 +37,11 @@ -(id)init { - (void)maisUmCueca { boy = boy + 1; + [delegate atualizaDados]; } - (void)maisUmaGata { girl++; + [delegate atualizaDados]; } -(int)getBoys { diff --git a/ContaPessoas/SecondViewController.h b/ContaPessoas/SecondViewController.h index f907831..091cba9 100644 --- a/ContaPessoas/SecondViewController.h +++ b/ContaPessoas/SecondViewController.h @@ -7,13 +7,15 @@ // #import +#import "Contador.h" -@interface SecondViewController : UIViewController +@interface SecondViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *totalBoys; @property (weak, nonatomic) IBOutlet UILabel *totalGirls; @property (weak, nonatomic) IBOutlet UILabel *total; - (IBAction)click:(id)sender; +//-(id)init; @end diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index 4894a6f..cdbb5ba 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -25,20 +25,38 @@ @interface SecondViewController () { @implementation SecondViewController +//-(id)init { +// self = [super init]; +// if (self) { +// contador = [Contador instancia]; +// contador.delegate = self; +// } +// return self; +//} + - (void)viewDidLoad { [super viewDidLoad]; contador = [Contador instancia]; + contador.delegate = self; + [self click:self]; } -- (void)viewDidAppear:(BOOL)animated{ - [self click:self];//Gambiarra. Please Understand -} +//- (void)viewDidAppear:(BOOL)animated{ +// [self click:self];//Gambiarra. Please Understand +//} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } +-(void) atualizaDados{ + //NSLog(@"Funcionando ae"); + _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; + _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal] ]; +} + - (IBAction)click:(id)sender { _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]];