Skip to content

Instantly share code, notes, and snippets.

@gegagome
Last active August 29, 2015 14:22
Show Gist options
  • Save gegagome/f9cb26d55e884870faf4 to your computer and use it in GitHub Desktop.
Save gegagome/f9cb26d55e884870faf4 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
#import "BBHHistoryTableView.h"
#import "SingletonClass.h"
@interface BBHHistoryViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
BBHHistoryTableView *historyTableView;
NSMutableArray *historyArray;
SingletonClass *singleton;;
}
@end
#import "BBHHistoryViewController.h"
#import "SingletonClass.h"
@interface BBHHistoryViewController () /* <BBHDelegate> */
//@property SingletonClass *singleton;
@end
@implementation BBHHistoryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
historyTableView = [[BBHHistoryTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:historyTableView];
[historyTableView.tableForHistory setDelegate:self];
[historyTableView.tableForHistory setDataSource:self];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// SingletonClass *singleton = [SingletonClass sharedInstance];
[singleton shareSeconds:singleton.seconds];
[self shareSeconds:singleton.seconds];
[self reloadTableData];
NSLog(@"BBHHistoryViewController seconds is : %d", singleton.seconds);
NSLog(@"viewWillAppear on BBHHistoryViewController");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title = @"History";
UIImage *i = [UIImage imageNamed:@"14-gear.png"];
self.tabBarItem.image = i;
}
historyArray = [[NSMutableArray alloc] init];
singleton = [SingletonClass sharedInstance];
if (singleton) {
NSLog(@"Singleton was init in %@", self.description);
}
return self;
}
# pragma mark - TableView
- (void)reloadTableData
{
[historyTableView.tableForHistory reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return historyArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [historyTableView.tableForHistory dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSDictionary *data = [historyArray objectAtIndex:indexPath.row];
NSString *txtForRow = [[data objectForKey:@"Value"] stringValue];
cell.textLabel.text = txtForRow;
return cell;
}
#pragma mark - SingletonClassDelegate
- (void)shareSeconds:(int)number
{
NSNumber *num = [NSNumber numberWithInt:number];
NSDictionary *dict = @{@"Value" : num};
[historyArray addObject:dict];
NSLog(@"amountOfSeconds is %@", dict);
}
//#pragma mark - NSArchive
//
//- (void)encodeWithCoder:(NSCoder *)aCoder {
//
//}
//
//- (id)initWithCoder:(NSCoder *)aDecoder {
//
// return
//}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import <UIKit/UIKit.h>
#import "SingletonClass.h"
#import "BBHDelegate.h"
#import "TimerObject.h"
@interface BBHHomeViewController : UIViewController
{
// SingletonClass *singleton;
}
//@property (nonatomic, weak)id<BBHDelegate>delegate;
@property int countValue;
@end
#import "BBHHomeViewController.h"
@interface BBHHomeViewController ()
@property (nonatomic)BOOL isTimerActive;
@property UILabel *countLabel;
@property UIButton *timerButton;
@property UIButton *endTimerButton;
@property UIButton *resetButton;
@property TimerObject *timerObject;
@end
@implementation BBHHomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Initialize NSTimer
// self.timerObject = [[TimerObject alloc] init];
[self configureView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.isTimerActive = NO;
self.tabBarItem.title = @"Home";
UIImage *i = [UIImage imageNamed:@"38-house.png"];
self.tabBarItem.image = i;
}
return self;
}
#pragma mark - CUSTOM INITIALIZERS
- (void)configureView
{
CGRect frameForCountLabel = CGRectMake(0, self.view.bounds.size.height/3, self.view.bounds.size.width, self.view.bounds.size.height/8);
CGRect frameForTimerButton = CGRectMake(0, frameForCountLabel.origin.y + 80, self.view.bounds.size.width, self.view.bounds.size.width/8);
CGRect frameForEndTimerButton = CGRectMake(0, frameForTimerButton.origin.y + 50, self.view.bounds.size.width, self.view.bounds.size.width/8);
CGRect frameForResetButton = CGRectMake(0, frameForEndTimerButton.origin.y + 50, self.view.bounds.size.width, self.view.bounds.size.width/8);
self.countLabel = [[UILabel alloc] initWithFrame:frameForCountLabel];
self.timerButton = [[UIButton alloc] initWithFrame:frameForTimerButton];
self.endTimerButton = [[UIButton alloc] initWithFrame:frameForEndTimerButton];
self.resetButton = [[UIButton alloc] initWithFrame:frameForResetButton];
self.countLabel.text = @"00:00";
self.countLabel.textAlignment = NSTextAlignmentCenter;
self.countLabel.font = [UIFont systemFontOfSize:40];
self.countLabel.textColor = [UIColor blackColor];
// Buttons
[self.timerButton setTitle:@"START" forState:UIControlStateNormal];
[self.endTimerButton setTitle:@"END" forState:UIControlStateNormal];
[self.resetButton setTitle:@"RESET" forState:UIControlStateNormal];
[self.timerButton addTarget:self action:@selector(timerStart) forControlEvents:UIControlEventTouchUpInside];
[self.endTimerButton addTarget:self action:@selector(timerEnd) forControlEvents:UIControlEventTouchUpInside];
[self.resetButton addTarget:self action:@selector(timerReset) forControlEvents:UIControlEventTouchUpInside];
self.timerButton.backgroundColor = [UIColor grayColor];
self.endTimerButton.backgroundColor = [UIColor lightGrayColor];
self.resetButton.backgroundColor = [UIColor lightGrayColor];
// Hide buttons
self.endTimerButton.hidden = YES;
self.resetButton.hidden = YES;
// Disable buttons
self.endTimerButton.enabled = NO;
self.resetButton.enabled = NO;
// Add views
[self.view addSubview:self.countLabel];
[self.view addSubview:self.timerButton];
[self.view addSubview:self.endTimerButton];
[self.view addSubview:self.resetButton];
}
#pragma mark - DEFAULT
#pragma mark - ACTIONS
- (void)timerStart
{
if (!self.isTimerActive) {
self.timerObject.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeCounter) userInfo:nil repeats:YES];
[self.timerButton setTitle:@"STOP" forState:UIControlStateNormal];
// Hide buttons
self.endTimerButton.hidden = YES;
self.resetButton.hidden = YES;
// Disable buttons
self.endTimerButton.enabled = NO;
self.resetButton.enabled = NO;
} else {
[self timerStopper];
[self.timerButton setTitle:@"RESUME" forState:UIControlStateNormal];
// Reveal buttons
self.endTimerButton.hidden = NO;
self.resetButton.hidden = NO;
// Enable buttons
self.endTimerButton.enabled = YES;
self.resetButton.enabled = YES;
}
self.isTimerActive = !self.isTimerActive;
}
- (void)timeCounter
{
self.timerObject.countValue++;
NSLog(@"timerCounter is : %i", self.timerObject.countValue);
NSLog(@"timerCounter is : %hhd", _isTimerActive);
self.countLabel.text = [NSString stringWithFormat:@"%i", self.timerObject.countValue];
if (self.timerObject) {
NSLog(@"Yes, timerObject exists");
} else {
NSLog(@"No, timerObject doesn't exist");
}
}
- (void)timerEnd
{
[self timerStopper];
[[SingletonClass sharedInstance] shareSeconds: self.timerObject.countValue];
NSLog(@"BBHHomeViewController seconds is : %d", self.timerObject.countValue);
NSLog(@"SingletonClass seconds is : %d", [[SingletonClass sharedInstance] seconds]);
[self timerReset];
}
- (void)timerReset
{
self.timerObject.countValue = 0;
self.countLabel.text = @"00:00";
}
- (void)timerStopper
{
[self.timerObject.timer invalidate];
NSLog(@"timerStopper");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import <Foundation/Foundation.h>
#import "BBHDelegate.h"
@interface SingletonClass : NSObject
+ (SingletonClass *)sharedInstance;
- (void)shareSeconds:(int)number;
@property int seconds;
//@property (nonatomic, weak)id<BBHDelegate>delegate;
@end
#import "SingletonClass.h"
@interface SingletonClass () <BBHDelegate>
@end
@implementation SingletonClass
+ (SingletonClass *)sharedInstance
{
static SingletonClass *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[SingletonClass alloc] init];
});
NSLog(@"SingletonClass");
return sharedInstance;
}
- (void)shareSeconds:(int)number
{
self.seconds = number;
NSLog(@"SingletonClass %@", [NSNumber numberWithInt:number]);
}
@end
#import <Foundation/Foundation.h>
@interface TimerObject : NSObject {
}
@property (weak, nonatomic)NSTimer *timer;
@property (assign)int countValue;
@property (nonatomic, retain)NSDate *date;
- (id)init;
@end
#import "TimerObject.h"
@implementation TimerObject :
- (id)init
{
self = [super init];
if (self) {
// self.timer = [[NSTimer alloc] init];
self.countValue = 0;
NSLog(@"TimerObject was initialized");
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment