Im a bit stuck and need some ideas.
I want to make an app where it counts up from 0-12 then another UILabel changes to 1. The first label starts again from 0-12 then the label changes to 2. So on...
I have tried a few ways adding in a NSTimer scheduledTimer and making a few selectors it worked to a certain extent but not to my liking.
I don't need any code examples (although would be nice) but just some ideas would be nice thanks. :)
do you want to change label according to time or by press button?
So you want to change label by timer so here it is.
Fist
#define TimeIntervelInSec 1.0
#define countMaxLimitForLower 12
Make 2 int in your .h file
int countLower;
int countHigher;
IBOutlet UILabel *lblLowerCount;
IBOutlet UILabel *lblHigherCount;
and in put this is in .m
- (void)viewDidLoad
{
[super viewDidLoad];
countLower = 0;
countHigher = 0;
[NSTimer scheduledTimerWithTimeInterval:TimeIntervelInSec
target:self
selector:@selector(timerChanged)
userInfo:nil
repeats:YES ];
}
-(void)timerChanged
{
if (countLower >= countMaxLimitForLower)
{
countHigher++;
countLower = 0;
}
else
{
countLower ++;
}
lblLowerCount.text = [NSString stringWithFormat:@"%d",countLower];
lblHigherCount.text = [NSString stringWithFormat:@"%d",countHigher];
}
@YashpalJavia was on the right track, but there are several issues with the proposed code:
Starting from that code:
Create an instance variable count, which will count total seconds since the timer is started.
- (void) startTimer;
{
count = 0;
[NSTimer scheduledTimerWithTimeInterval:TimeIntervelInSec
target:self
selector:@selector(timerChanged:)
userInfo:nil
repeats:YES ];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startTimer];
}
-(void)timerChanged: (NSTimer *) timer;
{
const int units = 12;
count++;
int countLower;
int countHigher;
countLower = count % units; //Use the modulo operator to get a value from 0 - 11
countUpper = countLower / units;
countLowerLabel.text = [NSString stringWithFormat: @"%d", countLower);
countUpperLabel.text = [NSString stringWithFormat: @"%d", countUpper);
}
The code above will make the lower value go from 0 to 11, then increment the higher value and reset the lower value back to 0. If you really want the lower to go from 0 to 12 (13 possible values) then change units to 13, but I bet that's not what you want.
If you counter increase in per second you can try something like this.
int count;
count=0;
[self performSelector:@selector(updateMyLabel:) withObject:nil afterDelay:12.0];
-(void)updateMyLabel:(id)sender
{
count++;
NSString *counterString = [NSString stringWithFormat:@"%d", count];
secondLabel.text=counterString;
}
I hope it will work for you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.