Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ContaPessoas.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
27A199D21A94D1FA008DC684 /* Contador.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Contador.m; sourceTree = "<group>"; };
27A199D41A94D22B008DC684 /* Contador.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Contador.h; sourceTree = "<group>"; };
27A199D51A94D821008DC684 /* ContadorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ContadorTests.m; sourceTree = "<group>"; };
4D4EDB891A97934400145B42 /* Mostrador.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mostrador.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -106,6 +107,7 @@
27A199B51A94B2A8008DC684 /* LaunchScreen.xib */,
27A199A31A94B2A8008DC684 /* Supporting Files */,
27A199D21A94D1FA008DC684 /* Contador.m */,
4D4EDB891A97934400145B42 /* Mostrador.h */,
27A199D41A94D22B008DC684 /* Contador.h */,
);
path = ContaPessoas;
Expand Down Expand Up @@ -438,6 +440,7 @@
27A199C81A94B2A8008DC684 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
27A199C91A94B2A8008DC684 /* Build configuration list for PBXNativeTarget "ContaPessoasTests" */ = {
isa = XCConfigurationList;
Expand All @@ -446,6 +449,7 @@
27A199CB1A94B2A8008DC684 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
2 changes: 2 additions & 0 deletions ContaPessoas/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright (c) 2015 Vinicius Miana. All rights reserved.
//



#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
Expand Down
7 changes: 7 additions & 0 deletions ContaPessoas/Contador.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> mostradorDelegate;

+(Contador *) contador;
- (void)maisUmCueca;
- (void)maisUmaGata;

-(int)getBoys;
-(int)getGirls;
-(int)getTotal;

@end

35 changes: 33 additions & 2 deletions ContaPessoas/Contador.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,40 @@
// Created by Vinicius Miana on 2/18/15.
// Copyright (c) 2015 Vinicius Miana. All rights reserved.
//
//Patricia Abreu

#import <Foundation/Foundation.h>
#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) {
Expand All @@ -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 {
Expand All @@ -39,6 +67,9 @@ -(int)getGirls {
return girl;
}

-(int)getTotal{
return boy + girl;
}


@end
Expand Down
2 changes: 1 addition & 1 deletion ContaPessoas/FirstViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
17 changes: 17 additions & 0 deletions ContaPessoas/Mostrador.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

//interface
@protocol MostradorDelegate <NSObject>

-(void)atualiza;

@end

4 changes: 3 additions & 1 deletion ContaPessoas/SecondViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//

#import <UIKit/UIKit.h>
#import "Mostrador.h"

@interface SecondViewController : UIViewController
//o esteriotipo informa que o SecondViewController implementa a interface MostradorDelegate
@interface SecondViewController : UIViewController<MostradorDelegate>

@property (weak, nonatomic) IBOutlet UILabel *totalBoys;
@property (weak, nonatomic) IBOutlet UILabel *totalGirls;
Expand Down
20 changes: 18 additions & 2 deletions ContaPessoas/SecondViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "SecondViewController.h"
#import "Contador.h"
#import "Mostrador.h"

@interface SecondViewController () {
Contador *contador;
Expand All @@ -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 {
Expand All @@ -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]];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ja vi este código em algum lugar. Como melhorar a reutilização?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um dica, sera que nao da para chamar este metodo dentro do click?

_totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]];
_total.text = [NSString stringWithFormat:@"%d", [contador getTotal] ];
}


Expand Down
10 changes: 10 additions & 0 deletions ContaPessoasTests/ContaPessoasTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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