Enabling and diasbling button if statement xcode - ios

I am looking to enable and disable a UIButton in XCode 7 using objective C. This is my code:
- (IBAction)startCount:(UIButton*)sender
{
countInt = 0;
self.Label.text = [NSString stringWithFormat:#"%i", countInt];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countTimer) userInfo:nil repeats:YES];
if (countInt > 0){
sender.enabled = false;
} else {
sender.enabled = true;
}
}
Any idea as to why when I run this, the button doesn't enable and disable when desired?

If you want to disable the button when countInt is greater then 0, you have to move the code that does it to countTimer function.
-(void)countTimer {
if (countInt > 0){
myButton.enabled = false;
} else {
myButton.enabled = true;
}
countInt += 1;
self.Label.text = [NSString stringWithFormat:#"%i", countInt];
}
But there's other ways to do this more efficiently.

Related

Keep label updating through segue

I have a small sample project i'm using to figure out how to implement this on my main project. This simple project has 2 VC's both with segues to each other.
On the initial VC is a button which leads to the TimerVC (both using the same class).
The TimerVC has a button and a label. When the button is pressed the label will increase by 1 every second.
If the timer is on and I segue back to the initial VC and then to the TimerVC the timer continues but the label stops updating.
How can I keep the label updating? The timer keeps going in the back-end but once the segue happens the label stops updating.
EDIT: Code provided below. The Timer is also more complex to represent a little of what I'm trying to do.
VC.H
NSTimer * timer;
NSTimer * updateTimer;
#property (weak, nonatomic) IBOutlet UIButton *idleOutler;
- (IBAction)idleAttack:(id)sender;
VC.M
int enemy001Hp = 100;
int deadEnemy = NO;
int noHealth = 0;
bool enemy001Active = NO;
- (void) enemy1 {
enemy001Active = YES;
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
}
- (void) enemyDamageTimer {
if (enemy001Active == YES) {
enemy001Hp -= 50;
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i",enemy001Hp];
}
}
- (void) updateLabelTimer {
if (enemy001Active == YES) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
}
}
- (void) stopTimer {
[timer invalidate];
timer = nil;
self.enemyHpLabel.text = [NSString stringWithFormat:#"0"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self enemy1];
if (enemy001Active) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
} else if (enemy002Active == YES) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy002Hp];
}
}
- (IBAction)idleAttack:(id)sender {
idleOn = YES;
self.idleOutler.hidden = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(enemyDamageTimer) userInfo:nil repeats:YES];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(updateLabelTimer) userInfo:nil repeats:YES];
if (enemy001Active == YES) {
if (enemy001Hp <= 0) {
[self enemy2];
enemy001Active = NO;
enemy002Active = YES;
}
} else if (enemy002Active == YES) {
if (enemy002Hp <= 0) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"0"];
enemy002Hp = 0;
enemy002Active = NO;
[self stopTimer];
}
}
}

Start and Stop functionality of StopWatch is not coordinated iOS

I am creating a stopwatch and the start and stop functionality is not coordinated. When the timer stops, it doesn't start where it left of.
The code looks like this:
self.endDate = [NSDate dateWithTimeIntervalSinceNow:12.0f*60.0f];
-(void)startPressed:(id)sender{
if(!_running){
_running = TRUE;
[sender setTitle:#"Stop" forState:UIControlStateNormal];
if (_stopTimer == nil) {
_stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(changeTimer:)
userInfo:nil
repeats:YES];
}
}else{
_running = FALSE;
[sender setTitle:#"Start" forState:UIControlStateNormal];
[_stopTimer invalidate];
_stopTimer = nil;
}
}
- (void)changeTimer:(NSTimer*)timer {
_timeInterval = [self.endDate timeIntervalSinceNow];
self.timerControl1.minutesOrSeconds = ((NSInteger)_timeInterval)%60;
self.timerControl2.minutesOrSeconds = (NSInteger)(_timeInterval/60.0f);
self.timerControl3.minutesOrSeconds = ((NSInteger)_timeInterval)%60;
}
I know it is because of timeIntervalSinceNow. But, how to change this? Need some guidance on this.
The problem:
When you stop the stopwatch and start it again after 3s for example, it shld still start back from the same timing. but both my code, the start and stop timing are different, they are starting from a different timing. Need help to solve this.
don't use the endDate in changeTimer but use the endDate only to init the timer
#import "RimsViewController.h"
#interface RimsViewController ()
#property(weak) IBOutlet UILabel *label;
#property(strong) NSDate *endDate;
#end
#implementation RimsViewController {
BOOL _running;
NSTimeInterval _timeInterval;
NSTimer *_stopTimer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.endDate = [NSDate dateWithTimeIntervalSinceNow:10*60];
_timeInterval = [self.endDate timeIntervalSinceNow];
}
-(IBAction)startPressed:(id)sender{
if(!_running){
_running = TRUE;
[sender setTitle:#"Stop" forState:UIControlStateNormal];
if (_stopTimer == nil) {
_stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(changeTimer:)
userInfo:nil
repeats:YES];
}
}else{
_running = FALSE;
[sender setTitle:#"Start" forState:UIControlStateNormal];
[_stopTimer invalidate];
_stopTimer = nil;
}
}
- (void)changeTimer:(NSTimer*)timer {
_timeInterval -= 1;// [self.endDate timeIntervalSinceNow];
NSUInteger secs = _timeInterval;
int h = (NSUInteger)secs/3600;
secs-=h*3600;
int m = (NSUInteger)secs/60;
secs-=m*60;
int s = secs;
self.label.text = [NSString stringWithFormat:#"%d / %d / %d", h, m, s];
}
#end

Cant get my NSTimer to end when the value <= 0

i cant seem to get my NSTimer to stop when it reaches 0 i've looked around for an answer and i tryed everything please help.
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
}
-(IBAction)start:(id)sender {
play.hidden = YES;
mainint = 60;
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(count) userInfo:nil repeats:YES];
if (timer1 == 0) {
[timer1 invalidate];
timer1 = nil;
}
}
put your if condition in count() function
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
if (mainint == 0) {
[timer1 invalidate];
}
}
The issue is that your timer will never be nil (0) unless you set it yourself to nil. I think what you're trying to achieve is something like this :
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
if (mainint <= 0) {
[timer1 invalidate];
timer1 = nil;
}
}

Update UILabel continuously — Objective-c

I'm trying to display the count of a timer in a UILabel. The timer works fine but I'm not able to update the label. The NSTimer is set up like this in Class1.
- (void)startTimer {
int timerCount = 5;
timer = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(timerDecrement)
userInfo:nil
repeats:YES];
[timer fire];
}
- (void)timerDecrement {
Class2 *cls2 = [[Class2 alloc] init];
timerCount = timerCount-1;
[cls2 updateTimeLeftLabel];
}
+ (int)getTimerCount {
return timerCount;
}
In Class2:
- (void)viewDidLoad {
if (timeLeftLabel == nil) {
timeLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 70, 120, 30)];
timeLeftLabel.text = #"Time left";
[self.view addSubview:timeLeftLabel];
}
- (void)updateTimeLeftLabel {
NSLog(#"%#", [NSString stringWithFormat:#"%i",
[Class1 getTimerCount]]);
timeLeftLabel.text = [NSString stringWithFormat:#"Time left: %i",
[Class1 getTimerCount]];
}
The correct timer value is logged but the UILabel isn't updated at all. What's wrong?
Try to make timerCount as a #property of your class or static.
You create a new instance of the Class2 and do not present it. You should store instance of Class2 somewhere and work with it.
I needed to update count on my UIButton so i have done like this
-(void)viewDidLoad {
self.myLeagueTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateCountdown) userInfo:nil repeats: YES];
}
- (int)timeIndervalRemain {
NSDate *currentTime = [NSDate date];
return (TIME_INTERVAL - [currentTime timeIntervalSinceDate:self.startTime]);
}
-(void) updateCountdown {
if ([self timeIndervalRemain] > 0) {
self.title = [NSString stringWithFormat:#"%d", [self timeIndervalRemain]];
} else {
[self timeExpire];
}
}
-(void) timeExpire {
if (_myLeagueTimer) {
[_myLeagueTimer invalidate];
_myLeagueTimer = nil;
}
}
You can simply pass the time left to the method updating the UILabel as a parameter:
- (void)updateTimeLeftLabel:(NSInteger)timeLeft {
timeLeftLabel.text = [NSString stringWithFormat:#"Time left: %i",
timeLeft];
}
- (void)timerDecrement {
Class2 *cls2 = [[Class2 alloc] init];
timerCount = timerCount-1;
[cls2 updateTimeLeftLabel:timerCount];
}

NSTimer Milliseconds countdown

I would like to make a simple countdown with seconds and millisecond: SS:MM.
However, i would like to stop the timer or do something when the timer reach 0:00.
Currently the timer works, but it doesnt stop at 0:00. I can make the seconds stop, but not the milliseconds. What is wrong?
-(void) setTimer {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = 99;
tmp.secondsCount = 2;
tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(timerRun) userInfo:nil repeats:YES];
}
-(void) timerRun {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
[tmp.countdownTimerGame invalidate];
tmp.countdownTimerGame = nil;
tmp.lives = tmp.lives - 1;
NSString *newLivesOutput = [NSString stringWithFormat:#"%d", tmp.lives];
livesLabel.text = newLivesOutput;
if (tmp.lives == 0) {
[self performSelector:#selector(stopped) withObject:nil];
}
else {[self setTimer]; }
}
else
tmp.milisecondsCount = 99;
}
NSString *timerOutput = [NSString stringWithFormat:#"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];
timeLabel.text = timerOutput;
}
-(void) stopped {
NSLog(#"Stopped game");
timeLabel.text = #"0:00";
}
Well. You do
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.milisecondsCount = 100;
tmp.secondsCount -= 1;
}
And right after that
if ((tmp.secondsCount == 0) && tmp.milisecondsCount == 0) {
//stuff
}
How could it ever happen that they're both 0 if, as soon as milisecond reaches 0, you reset it to 100?
EDIT: Do instead something like:
if(tmp.milisecondsCount < 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
}
else
tmp.milisecondsCount = 99;
}
In your code, a first condition is met
if(tmp.milisecondsCount == 0){
tmp.milisecondsCount = 100;
so that the next conditional statment
&& tmp.milisecondsCount == 0
will never be true.

Resources