I want a ranking in my app where you can see who has the fastest lap.
I try to convert the time into milliseconds, but it didn't work well.
This is my code at the moment.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize watch,start,reset;
- (void)viewDidLoad {
[super viewDidLoad];
running = NO;
count = 0;
watch.text = #"00:00.00";
start.layer.cornerRadius = 45;
reset.layer.cornerRadius = 45;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)startpressed:(id)sender {
if (running == NO) {
running = YES;
[start setTitle:#"STOPP" forState:UIControlStateNormal];
NSDate *watch = [NSDate dateWithTimeIntervalSince1970:(1273636800 / 1000.0)];
if (myTimer == nil) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.0055
target:self
selector:#selector(updateTimer)
userInfo: nil
repeats:YES];
}
} else {
running = NO;
[myTimer invalidate];
myTimer = nil;
[start setTitle:#"START" forState:UIControlStateNormal];
}
}
- (IBAction)resetpressed:(id)sender {
running =NO;
[myTimer invalidate];
myTimer =nil;
[start setTitle:#"START" forState:UIControlStateNormal];
count = 0;
watch.text = #"00:00.00";
}
- (void)updateTimer {
count++;
int min = floor(count/100/60);
int sec = floor(count/100);
int mSec = count % 100;
if (sec >= 60) {
sec = sec % 60;
}
watch.text = [NSString stringWithFormat:#"%02d:%02d.%02d", min,sec,mSec];
}
#end
this is my ViewController.h code:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSTimer *myTimer;
BOOL running;
int count;
double methodStart;
}
#property (weak, nonatomic) IBOutlet UILabel *watch;
#property (weak, nonatomic) IBOutlet UIButton *start;
#property (weak, nonatomic) IBOutlet UIButton *reset;
#property (weak, nonatomic) IBOutlet UILabel *eins;
#property (weak, nonatomic) IBOutlet UILabel *zwei;
#property (weak, nonatomic) IBOutlet UILabel *drei;
- (IBAction)startpressed:(id)sender;
- (IBAction)resetpressed:(id)sender;
- (void) updateTimer;
#end
Create array for holding lap history in class interface/extension and initialise it.
self.lapHistory = [#[] mutableCopy];
Capture and sort every lap time when lap is reset/stopped
- (IBAction)resetpressed:(id)sender {
[self.lapHistory addObject:#(count)];
self.lapHistory = [self.lapHistory sortedArrayUsingSelector: #selector(compare:)];
}
Display sorted ranking from lapHistory
-(void)displayRanking{
NSMutableString *rankingResult = [[NSMutableString alloc] init];
for (NSNumber *lap in self.lapHistory) {
[rankingResult appendString:[NSString stringWithFormat:#"%ld\n",[lap integerValue]]];
}
NSLog(#"Ranking result is %#", rankingResult);
}
Related
I want to set multiple running timer value in tableview so user easily checked how many time is remaining and stop any specific timer.
Multiple Timer Start and updated value display in NSLog perfectly.
User set NSTimer for particular recipe.
Now suppose user set timer for recipe1 in recipedetail and now back from that recipedetail.
Then again set timer for recipe2 in recipedetail and back from that recipedetail.
and i want to All timer value display in UITableView. And tableview placed on recipedetail screen.
So my point is TimerUpdated value display in NSLog but not display in tableview because every Recipedetails screen generate new object of UITableView So value is not updated perfactly
-(IBAction)okBtn:(id)sender
{
[self countdownTimer];
}
-(void)countdownTimer {
[[NSUserDefaults standardUserDefaults]setObject:recipeboxId forKey:[NSString stringWithFormat:#"timer_%#",recipeboxId]];
[[NSUserDefaults standardUserDefaults]synchronize];
// set timer
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
(theAppDelegate).timer = [NSTimer scheduledTimerWithTimeInterval:0.6f
target:self
selector:#selector(updateCounter:)
userInfo:recipeboxId
repeats:YES];
[(theAppDelegate).timer fire];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];//Timer run in background
[self CreateLocalNotification];
}
-(void)calculateTimeFromPicker
{
NSString *hoursStr = [NSString stringWithFormat:#"%#",[hoursArray objectAtIndex:[self.picker_timer selectedRowInComponent:0]]];
NSString *minsStr = [NSString stringWithFormat:#"%#",[minsArray objectAtIndex:[self.picker_timer selectedRowInComponent:1]]];
NSString *secsStr = [NSString stringWithFormat:#"%#",[secsArray objectAtIndex:[self.picker_timer selectedRowInComponent:2]]];
int hoursInt = [hoursStr intValue];
int minsInt = [minsStr intValue];
int secsInt = [secsStr intValue];
interval = secsInt + (minsInt*60) + (hoursInt*3600);
secondsLeft=interval;
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 )
{
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
[[NSUserDefaults standardUserDefaults]setInteger:secondsLeft forKey:[NSString stringWithFormat:#"secondsLeft_%#",recipeboxId]];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"timer :%#",[NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds]);
NSString *timer_updated_value=[NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
[[NSUserDefaults standardUserDefaults]setObject:timer_updated_value forKey:[NSString stringWithFormat:#"updated_timer_%#",[(theAppDelegate).timer userInfo]]];
[[NSUserDefaults standardUserDefaults]synchronize];
recipeArr = [[(theAppDelegate).Timer_recipeIdArr reverseObjectEnumerator] allObjects];
for (int section = 0; section < [recipeArr count]; section++)
for (int section = 0; section < [recipeArr count]; section++)
{
for (int row = 0; row < (int)[self.timerWindowTbl numberOfRowsInSection:section]; row++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [self.timerWindowTbl cellForRowAtIndexPath:indexPath];
for(UILabel *lbl in [cell.contentView subviews])
{
if([lbl isKindOfClass:[UILabel class]])
{
if(lbl.tag == 1)
{
NSString *timer_desc= [[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:#"timerDescString_%#",recipeArr[indexPath.row]]];
NSString *recipe_title=[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:#"timerRecipeTitle_%#",recipeArr[indexPath.row]]];
NSString *updated_timer=[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:#"updated_timer_%#",recipeArr[indexPath.row]]];
NSString *desc=[NSString stringWithFormat:#"%#\n'%#\' %# %#.",recipe_title,timer_desc,NSLocalizedString(#"is done in",nil),updated_timer];
lbl.text=updated_timer;
[lbl setNeedsDisplay];
NSLog(#"*******************************lbl.text:%#",lbl.text);
lbl.numberOfLines=0;
[lbl sizeToFit];
for(UIButton *btn in [cell.contentView subviews])
{
if([btn isKindOfClass:[UIButton class]])
{
if(btn.tag==2)
{
btn.titleLabel.text=[NSString stringWithFormat:#"%#",recipeArr[indexPath.row]];
}
}
}
break;
}
}
}
}
}
}
else
{
secondsLeft = hours = minutes = seconds = 0;
[self TimerInvalidate];
NSLog(#"Time out :");
}
}
i think u need to refractor your code, u need reload the tableview cell each time when recipe timer stared for particular recipe
this is sample demo u can try with separate project,
first create a Recipe class subclass of NSObject like below,
in Recipe .h file
#import <Foundation/Foundation.h>
#class Recipe;
#protocol RecipeDelegate <NSObject> //we need to notify which recipe time changing
- (void)timerChangedInRecipe:(Recipe *)recipe;
#end
#interface Recipe : NSObject
#property (nonatomic, assign) NSInteger recipeboxId;
#property (nonatomic, strong) NSString *recipeName;
#property (nonatomic, strong) NSString *countTimer;
#property (nonatomic, assign) NSTimeInterval secondsLeft;
#property (nonatomic, assign) NSTimeInterval interval;
#property (nonatomic, assign) int hours;
#property (nonatomic, assign) int minutes;
#property (nonatomic, assign) int seconds;
#property (nonatomic, strong) NSTimer *timer;
#property (nonatomic, assign) id<RecipeDelegate>delegate;
- (id)initWithRecipieId:(NSInteger)recipeId recipeName:(NSString *)name;
- (void )currentTimerString;
- (void)startTimer; //use to start the timer
- (void)stopTimer; //stop the timer
#end
in Recipe.m file
#import "Recipe.h"
#implementation Recipe
- (id)init
{
self = [super init];
if(self)
{
}
return self;
}
- (id)initWithRecipieId:(NSInteger)recipeId recipeName:(NSString *)name
{
self = [super init];
if(self)
{
self.recipeboxId = recipeId;
self.recipeName = name;
}
return self;
}
- (void)currentTimerString //this is similar to your method updateCounter:
{
self.secondsLeft -- ;
if(self.secondsLeft > 0 )
{
_hours = (int)self.secondsLeft / 3600;
_minutes = ((int)self.secondsLeft % 3600) / 60;
_seconds = ((int)self.secondsLeft %3600) % 60;
self.countTimer = [NSString stringWithFormat:#"%02d:%02d:%02d", self.hours, self.minutes, self.seconds];
if([self.delegate respondsToSelector:#selector(timerChangedInRecipe:)])
[self.delegate timerChangedInRecipe:self]; //notifying the tableview to reload the particular cell
}
else
{
_hours = _minutes = _seconds = 0;
[self stopTimer]; //timer finished stop the timer
}
}
- (void)startTimer
{
if(_timer == nil)
_timer = [NSTimer scheduledTimerWithTimeInterval:0.6f target:self selector:#selector(currentTimerString) userInfo:nil repeats:YES];
else
[_timer fire];
}
- (void)stopTimer
{
if(_timer)
[self.timer invalidate];
self.timer = nil;
}
#end
we are created the recipe object, this will handle the timer and notification to tableview, starting the timer and stoping the timer you can add some other functionalities if u want to.. :)
in ViewController.h file
#import <UIKit/UIKit.h>
#import "Recipe.h" //import the recipe class
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,RecipeDelegate>
#property (weak, nonatomic) IBOutlet UITableView *timerWindowTbl;
#property (nonatomic, strong) NSMutableArray *recipeArr; //array to hold the recipes
#end
in ViewController.m file
#import "ViewController.h"
#import "RecipeDetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_recipeArr = [[NSMutableArray alloc] init];
[self populateRecipe]; //i am simply creating the recipes hear for testing
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)populateRecipe
{
for(NSInteger k = 0 ; k < 10 ; k++)
{
Recipe *recipe = [[Recipe alloc] initWithRecipieId:k recipe eName:[NSString stringWithFormat:#"recipe_%ld",(long)k]];
[self.recipeArr addObject:recipe];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//just configure your tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.recipeArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RECIPEE_CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"RECIPEE_CELL"];
}
Recipe *recipe = self.recipeArr[indexPath.row];
recipe.delegate = self;
cell.textLabel.text = recipe.recipeName;
cell.detailTextLabel.text = recipe.countTimer;
return cell;
}
//hear go to recipe detail controller and set timer
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Recipe *selRecipe = self.recipeArr[indexPath.row];
RecipeDetailViewController *recipeDetailController = [[RecipeDetailViewController alloc] initWithNibName:#"RecipeDetailViewController" bundle:nil];
recipeDetailController.selectedRecipe = selRecipe;
[self.navigationController pushViewController:recipeDetailController animated:YES];
}
//this is the delegate method from recipe class
//this method is responsible for reloading the particular cell
- (void)timerChangedInRecipe:(Recipe *)recipe
{
NSInteger index = recipe.recipeboxId;
NSIndexPath *rowPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.timerWindowTbl reloadRowsAtIndexPaths:#[rowPath] withRowAnimation:UITableViewRowAnimationNone];
}
#end
and in detail controller same as your recipe detail controller RecipeDetailViewController.h file
#import <UIKit/UIKit.h>
#import "Recipe.h"
#interface RecipeDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIDatePicker *timePicker;
#property (weak, nonatomic) IBOutlet UIButton *okButton;
#property (weak, nonatomic) IBOutlet UILabel *timerLabel;
#property (nonatomic, strong) Recipe *selectedRecipe;
- (IBAction)okBtn:(id)sender;
#end
and in RecipeDetailViewController .m file,
#import "RecipeDetailViewController.h"
#interface RecipeDetailViewController ()
#end
#implementation RecipeDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[_timePicker addTarget:self action:#selector(timerChanged:) forControlEvents:UIControlEventValueChanged]; //i am setting a sample picker
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)timerChanged:(UIDatePicker *)datePickerView
{
NSTimeInterval duration = datePickerView.countDownDuration;
int hours = (int)(duration/3600.0f);
int minutes = ((int)duration - (hours * 3600))/60;
int seconds = minutes/60;
_selectedRecipe.interval = seconds + (minutes*60) + (hours*3600);
_selectedRecipe.secondsLeft =_selectedRecipe.interval;
_timerLabel.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
_selectedRecipe.interval = duration;
}
- (IBAction)okBtn:(id)sender //hear start the timer particular recipe
{
[_selectedRecipe startTimer];
}
#end
Edit in the source code u are shared
the problem in the cell type i think, if you want u can subclass the tableview cell and place the timer labels in the required position,
so in your code, ViewController.m file replace below method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RECIPEE_CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"RECIPEE_CELL"];
}
Recipe *recipe = self.recipeArr[indexPath.row];
recipe.delegate = self;
cell.textLabel.text = recipe.recipeName;
cell.detailTextLabel.text = recipe.countTimer;
NSLog(#"timer in cell :%#",cell.detailTextLabel.text);
return cell;
}
and in the RecipeDetailViewController.m file replace,
//no need to wait for value change
- (IBAction)okBtn:(id)sender //hear start the timer particular recipe
{
NSTimeInterval duration = _timePicker.countDownDuration;
_selectedRecipe.interval = duration;
[_selectedRecipe startTimer];
}
and also change the timePicker mode to Count Down Timer in attribute inspector
u can download the edited code hear
Put UI related changes in main queue for better understanding show given below code. You can also put other UI related changes in Main_queue
dispatch_async(dispatch_get_main_queue(), ^{
lbl.text=updated_timer;
});
I'm populating an object's fields from Labels, when displaying in Log the label is the correct value, but the object's field is null. I'm coming over from an Android/ Java background and this is just awkward.
Any help would be great.
To be clear, the "soil type field" log shows "example"while the "soil type" log shows (null)
- (IBAction)saveButton:(id)sender {
Soil *thisSoil = self.thisSoil;
thisSoil.soilType = self.soilNameField.text;
NSLog(#"soil type field %#", self.soilNameField.text);
NSLog(#"soil type: %#", thisSoil.soilType);
thisSoil.frictionAngle = [self.frictionAngleValue.text integerValue];
if ([self.soilUnitsSwitch isOn] ) {
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}else{
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}
[self.delegate SoilCreatorViewController:self didFinishItem:thisSoil];
[self.navigationController popViewControllerAnimated:YES];
}
The entire controller .m file
#import "SoilCreatorViewController.h"
#define IMPERIAL_TO_METRIC 0.3048
#define KG_TO_LBS 2.2
#interface SoilCreatorViewController ()
#end
#implementation SoilCreatorViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)soilQuestions:(id)sender {
[self popupmaker :#"Friction Angle" : #"Phi is the angle of internal friction for soil, which governs soil strength and resistance. This value should be attained from competent field testing and the judgment of a licensed engineer."];
[self popupmaker :#"Soil Cohesion": #"Cohesion defines the non-stress dependent shear strength of soil and should be used with caution. Typically,cohesion occurs in stiff, over-consolidated clays or cemented native soils. Cohesion should be neglected if the designer is unsure of its presence."];
}
- (IBAction)SwitchDidChange:(id)sender {
if ([sender isOn]) {
self.cohesionUnits.text = #"Ft";
self.unitWeightUnits.text= #"M";
}else{
self.cohesionUnits.text = #"M";
self.unitWeightUnits.text = #"M";
}
}
- (IBAction)unitWtDidChange:(id)sender {
self.unitWeightValue.text = [NSString stringWithFormat:#"%.1f", (double)self.unitWeightStepper.value];
}
- (IBAction)frictionAngleDidChange:(id)sender {
self.frictionAngleValue.text = [NSString stringWithFormat:#"%d", (int)self.frictionAngleStepper.value];
}
- (IBAction)cohesionDidChange:(id)sender {
self.cohesionValue.text = [NSString stringWithFormat:#"%.1f", (double)self.cohesionStepper.value];
}
- (IBAction)textFieldDismiss:(id)sender {
[[self view] endEditing:YES];
}
- (IBAction)UnitSwitch:(id)sender {
if ([sender isOn]) {
self.unitWeightUnits.text = #"LBS/cubic Ft.";
self.cohesionUnits.text = #"imp";
}else{
self.unitWeightUnits.text = #"KG/m3";
self.cohesionUnits.text = #"met";
}
}
- (IBAction)cancelButton:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)saveButton:(id)sender {
Soil *thisSoil = self.thisSoil;
thisSoil.soilType = self.soilNameField.text;
NSLog(#"soil type field %#", self.soilNameField.text);
NSLog(#"soil type: %#", thisSoil.soilType);
thisSoil.frictionAngle = [self.frictionAngleValue.text integerValue];
if ([self.soilUnitsSwitch isOn] ) {
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}else{
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}
[self.delegate SoilCreatorViewController:self didFinishItem:thisSoil];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)popupmaker:(NSString *)title :(NSString *)message{
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil
];
[alert show];
}
#end
The .h file
#import "Soil.h"
#class SoilCreatorViewController;
#protocol SoilCreatorViewDelegate <NSObject>
-(void)SoilCreatorViewController:(SoilCreatorViewController *)controller didFinishItem:(Soil *)item;
#property (nonatomic, weak) id <SoilCreatorViewDelegate> delegate;
#end
#import <UIKit/UIKit.h>
#import "CalculationDetailViewController.h"
#interface SoilCreatorViewController : UIViewController
#property (nonatomic,weak) id<SoilCreatorViewDelegate> delegate;
#property (weak, nonatomic) IBOutlet UITextField *soilNameField;
#property (weak, nonatomic) IBOutlet UISwitch *soilUnitsSwitch;
#property (nonatomic,strong) Soil *thisSoil;
#property (weak, nonatomic) IBOutlet UIStepper *frictionAngleStepper;
#property (weak, nonatomic) IBOutlet UIStepper *unitWeightStepper;
#property (weak, nonatomic) IBOutlet UIStepper *cohesionStepper;
#property (weak, nonatomic) IBOutlet UILabel *unitWeightValue;
#property (weak, nonatomic) IBOutlet UILabel *frictionAngleValue;
#property (weak, nonatomic) IBOutlet UILabel *cohesionValue;
#property (weak, nonatomic) IBOutlet UILabel *unitWeightUnits;
#property (weak, nonatomic) IBOutlet UILabel *cohesionUnits;
- (IBAction)soilQuestions:(id)sender;
- (IBAction)SwitchDidChange:(id)sender;
- (IBAction)unitWtDidChange:(id)sender;
- (IBAction)frictionAngleDidChange:(id)sender;
- (IBAction)cohesionDidChange:(id)sender;
- (IBAction)textFieldDismiss:(id)sender;
- (IBAction)cancelButton:(id)sender;
- (IBAction)saveButton:(id)sender;
-(void)popupmaker:(NSString *)title :(NSString *)message;
#end
I don't see any code that sets up thisSoil. At some point in your code, you need to write something like
self.thisSoil = [[Soil alloc] init];
or self.thisSoil will stay nil forever.
In my iOS app I have a custom UITableViewCell that is part of a TableView in my FirstViewController. In the FirstViewController I have an array of objects that corresponds to populating the rows and then of course the objects in that array which have properties.
I have a timer in each cell and I need to get the length of the timer from a property of the corresponding object. I've tried importing FirstViewController.h but I get the error "property not found on object of type" no matter what.
I already have the object created and configured in the cellForRowAtIndexPath method of the FirstViewController but I was hoping to use it in a method in TableViewCell.m.
Is there a way I can use the array or the object in the TableViewCell? Or should the method be implemented in the FirstViewController?
EDIT #1:
I moved cell.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:cell selector:#selector(startTimer) userInfo:nil repeats:YES]; into the cellForRow... method of the FirstViewController. Now the timer runs with the desired time but it's no longer started by a button. It immediately runs when the TableView is loaded and restarts every time the TableView is reloaded.
Here is my ATCTableViewCell.h file: (Updated with Edit #2)
#import <UIKit/UIKit.h>
#import "ATCFirstViewController.h"
#interface ATCTableViewCell : UITableViewCell
- (void)startTimer;
// Contents moved to cellForRow... -> - (IBAction)playTimer:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#property (weak, nonatomic) IBOutlet UILabel *timeLabel;
#property (weak, nonatomic) IBOutlet UIButton *playButton;
#property (weak, nonatomic) IBOutlet UIButton *pauseButton;
#property NSTimer *timer;
#property int secondsCount; // initially the length in seconds and then decreased
#end
EDIT #2:
The timer is a decrementing timer that changes a label in the startTimer method which is in ATCTableViewCell.m. The label is a property of the cell at the moment along with another label and the buttons. I'll work on moving things to the object class instead of having them as properties the cell.
Here is the startTimer method:
- (void)startTimer {
self.secondsCount--;
int hours = self.secondsCount / 3600;
int minutes = (self.secondsCount / 60) - (hours * 60);
int seconds = self.secondsCount - (hours * 3600) - (minutes * 60);
NSString *timerOutput = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
self.timeLabel.text = timerOutput;
if (self.secondsCount == 0) {
[self.timer invalidate];
self.timer = nil;
}
}
Here is my ATCObject.h file:
#import <Foundation/Foundation.h>
#interface ATCObject : NSObject
#property NSString *title;
#property int lengthInSeconds;
#property int initialHours;
#property int initialMinutes;
#end
Thanks
I am updating this answer based on all of the previous discussion.
#interface ATCObject : NSObject
#property (nonatomic, strong) NSString *title;
#property (nonatomic, assign) NSInteger lengthInSeconds;
#property (nonatomic, assign) NSInteger initialHours;
#property (nonatomic, assign) NSInteger initialMinutes;
//Move the NSTimer and the -playTimer method to this class
#property (nonatomic,strong) NSTimer *timer;
-(void)startTimer;
-(void)timerTick;
-(void)pauseTimer;
#end
In your tableView controller class:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Identifier";
ATCTableViewCell *cell = (ATCTableViewCell *)[tableView dequeueReusableCellForIdentifer:Identifier];
if (!cell) {
cell = [[ATCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:Identifier];
[cell.playButton addTarget:self action:#selector(playTouched:) forControlEvents:UIControlEventTouchUpInside];
[cell.pauseButton addTarget:self action:#selector(pauseTouched:) forControlEvents:UIControlEventTouchUpInside];
}
ATCObject *objectForRow = [myDataArray objectAtIndex:indexPath.row];
cell.timeLabel.text = objectForRow.title;
return cell;
}
//Also in your controller class
-(void)playTouched:(id)sender {
//you may need to change self.tableView to however you reference the table in your class
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil) {
ATCObject *currentObject = [myDataArray objectAtIndex:indexPath.row];
[currentObject startTimer];
}
}
//ATCObject.m
-(void)startTimer {
if (!self.timer) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerTick:)
userInfo:nil
repeats:YES];
}
}
-(void)timerTick {
//Already assuming lengthInSeconds is set
if (lengthInSeconds > 0) {
lengthInSeconds--;
//Do your math to get total hours and minutes
NSInteger totalHours = //math stuff convert lengthInSeconds
NSInteger totalMinutes = //more math stuff etc
NSString *newTitle = [NSString stringWithFormat:#"%i:%i",totalHours,totalMinutes];
self.title = newTitle;
} else {
self.title = #"00:00";
[self.timer invalidate];
self.timer = nil;
}
}
I want to change label.text but when I do it and check it with a NSLog it returns me a (null).
Here's my code:
NextViewController.h
#interface NextViewController (){
NSTimer *timerTen;
NSInteger countDown;
NSString *timer;
}
#end
#implementation NextViewController
static UILabel *lblTest;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)startCountDown{
countDown = 10;
timerTen = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(dismissView) userInfo:nil repeats:YES];
}
-(void)dismissView{
timer = [NSString stringWithFormat:#"%ld", (long)countDown];
_lbl_countDown.text = timer;
NSLog(#"\nCountDown: %#\n", self.lbl_countDown.text);
countDown--;
if (countDown <= 0) {
SectionViewController *sectionView = [[SectionViewController alloc] init];
[sectionView.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
}
Then I have a XIB file with a label and more things, so I've used a prop right there in the .h to change it from other classes, but I can't change it.
NextViewController.h
#import <UIKit/UIKit.h>
#interface NextViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *lbl_section;
#property (strong, nonatomic) IBOutlet UILabel *lbl_nextSection;
#property (strong, nonatomic) IBOutlet UILabel *lbl_countDown;
-(void)startCountDown;
#end
Hope anyone can help me cause I don't know what is happening here.
Thanks.
Call your method in ViewDidLoad or ViewWillAppear
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self startCountDown]
}
OR
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startCountDown]
}
Try to call the function first using [self startCountDown] in the ViewDidLoad method
if the problem persist:
Try to use this : [_lbl_count setText#"myText"];
In the Identity Inspector, click on the label uncheck the 'Static Text' and check 'Enabled' and 'User Interaction Enabled'
Finally check that your variable timer is not null after your initialization.
timer = [NSString stringWithFormat:#"%ld", (long)countDown];
Hope that will be helpfull
I wrote this code to use a button tap to start a countdown from 10 to a segue:
In my h:
#property (strong, nonatomic) IBOutlet UILabel *countdownCounter;
#property (strong, nonatomic) IBOutlet UIButton *countdownStarter;
In my m:
static NSTimer *timer;
static int remainingCounts;
.
.
-(void)countDown
{
if (--remainingCounts == 0)
{
[timer invalidate];
timer = nil;
[self performSegueWithIdentifier:#"FromStartToBalancer" sender:self];
}
else
{
NSString * holdText = [NSString stringWithFormat:#"%d", remainingCounts];
[self.countdownCounter setText:holdText];
}
}
- (IBAction)goToBalancerPressed:(UIButton *)sender
{
self.countdownStarter.userInteractionEnabled = NO;
self.countdownStarter.alpha = .3;
remainingCounts = 10;
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(countDown)
userInfo:nil
repeats:YES];
}
I am working on a scoreboard of sorts. The player can adjust three different values (gear, level, and bonuses) that when added should provide a total strength. Each of these values is currently being output as an integer and a UILabel displays its' respective integer. I cannot figure out how to add all three integers and then display them on a UILabel. I am currently developing for iOS 7 but I don't imagine this to be terribly different for current supported OS's. Any help is greatly appreciated.
.h
#import <UIKit/UIKit.h>
int levelCount;
int gearCount;
int oneShotCount;
int totalScoreCount;
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *totalScore;
#property (weak, nonatomic) IBOutlet UILabel *playerName;
#property (weak, nonatomic) IBOutlet UILabel *levelNumber;
#property (weak, nonatomic) IBOutlet UILabel *gearNumber;
#property (weak, nonatomic) IBOutlet UILabel *oneShotNumber;
- (IBAction)levelUpButton:(id)sender;
- (IBAction)levelDownButton:(id)sender;
- (IBAction)gearUpButton:(id)sender;
- (IBAction)gearDownButton:(id)sender;
- (IBAction)oneShotUpButton:(id)sender;
- (IBAction)oneShotDownButton:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
int ans = levelCount + gearCount + oneShotCount;
self.levelNumber.text = [NSString stringWithFormat:#"%i", ans];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)levelUpButton:(id)sender {
levelCount = levelCount + 1;
self.levelNumber.text = [NSString stringWithFormat:#"%i", levelCount];
}
- (IBAction)levelDownButton:(id)sender {
levelCount = levelCount - 1;
self.levelNumber.text = [NSString stringWithFormat:#"%i", levelCount];
}
- (IBAction)gearUpButton:(id)sender {
gearCount = gearCount + 1;
self.gearNumber.text = [NSString stringWithFormat:#"%i", gearCount];
}
- (IBAction)gearDownButton:(id)sender {
gearCount = gearCount - 1;
self.gearNumber.text = [NSString stringWithFormat:#"%i", gearCount];
}
- (IBAction)oneShotUpButton:(id)sender {
oneShotCount = oneShotCount + 1;
self.oneShotNumber.text = [NSString stringWithFormat:#"%i", oneShotCount];
}
- (IBAction)oneShotDownButton:(id)sender {
oneShotCount = oneShotCount - 1;
self.oneShotNumber.text = [NSString stringWithFormat:#"%i", oneShotCount];
}
#end
Create some sort of updateScore method that is called anytime one of the other values change.
- (void)updateScore {
totalScoreCount = ... // calculate score from levelCount, gearCount, and oneShotCount
self.totalScore.text = [NSString stringWithFormat:#"%i", totalScoreCount];
}
Then in each of your ...UpButton: and ...DownButton: methods you call:
[self updateScore];
Be sure to call this after updating the other value first. Example:
- (IBAction)levelUpButton:(id)sender {
levelCount = levelCount + 1;
self.levelNumber.text = [NSString stringWithFormat:#"%i", levelCount];
[self updateScore];
}