Ios 在目标c中加载视图时,标签中的值从1更改为100
我想向用户显示,最初的值是0,然后增加到1,然后增加到2,然后在几秒钟后重复序列 由我完成的工作 我在viewdidload中编写了此代码,但它直接在label中打印99,但我希望显示用户将值从0更改为99Ios 在目标c中加载视图时,标签中的值从1更改为100,ios,objective-c,Ios,Objective C,我想向用户显示,最初的值是0,然后增加到1,然后增加到2,然后在几秒钟后重复序列 由我完成的工作 我在viewdidload中编写了此代码,但它直接在label中打印99,但我希望显示用户将值从0更改为99 for(int i=0;i<100;i++){ _totalEffort.text=[NSString stringWithFormat:@"%d",i]; } for(int i=0;i根据您的要求,您需要使用NSTimer更新标签进度 步骤1:在@接口中创建2个变
for(int i=0;i<100;i++){
_totalEffort.text=[NSString stringWithFormat:@"%d",i];
}
for(int i=0;i根据您的要求,您需要使用NSTimer
更新标签进度
步骤1:在@接口中创建2个变量
@interface BUViewController: UIViewController {
NSTimer *timerCount;
int progress;
}
步骤2:使用进度值0
在viewDidLoad()中启动计时器
progress = 0;
timerCount = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
步骤3:使用计时器更新标签文本创建方法,检查其中的进度值,并在计时器到达100
时停止计时器
-(void)updateLable{
if (progress<100) {
lblProgress.text = [NSString stringWithFormat:@"%d",progress];
progress++;
}else{
[timerCount invalidate];
timerCount = nil;
}
}
-(void)可更新{
如果(进度根据您的要求,您需要使用NSTimer
更新标签进度
步骤1:在@接口中创建2个变量
@interface BUViewController: UIViewController {
NSTimer *timerCount;
int progress;
}
步骤2:使用进度值0
在viewDidLoad()中启动计时器
progress = 0;
timerCount = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
步骤3:使用计时器更新标签文本创建方法,检查其中的进度值,并在计时器到达100
时停止计时器
-(void)updateLable{
if (progress<100) {
lblProgress.text = [NSString stringWithFormat:@"%d",progress];
progress++;
}else{
[timerCount invalidate];
timerCount = nil;
}
}
-(void)可更新{
如果(progress为此,您可以使用NSTimer
您需要在其选择器
方法中更新UILabel
值。您可以使用此代码
-(void)viewDidLoad{
[super viewDidLoad];
self.time = 0;
_labelTimer.text =@"0";
[self startTimer];
}
- (void)startTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)timerAction:(NSTimer *)timer {
self.time++;
if (self.time == 100)
{
[self stopTimer];
}
_labelTimer.text = [NSString stringWithFormat:@"%d",self.time];
}
-(void)stopTimer{
[self.timer invalidate];
self.timer = nil;
}
为此,您可以使用NSTimer
您需要在其选择器
方法中更新UILabel
值。您可以使用此代码
-(void)viewDidLoad{
[super viewDidLoad];
self.time = 0;
_labelTimer.text =@"0";
[self startTimer];
}
- (void)startTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)timerAction:(NSTimer *)timer {
self.time++;
if (self.time == 100)
{
[self stopTimer];
}
_labelTimer.text = [NSString stringWithFormat:@"%d",self.time];
}
-(void)stopTimer{
[self.timer invalidate];
self.timer = nil;
}
我认为这将有助于你:
@interface ViewController (){
int i;
NSTimer *myTimer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(setData)
userInfo:nil
repeats:YES];
i = 0;
}
-(void)setData{
_totalEffort.text=[NSString stringWithFormat:@"%d",i];
i = i + 1;
if(i == 100){
[myTimer invalidate];
myTimer = nil;
}
}
我认为这将有助于你:
@interface ViewController (){
int i;
NSTimer *myTimer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(setData)
userInfo:nil
repeats:YES];
i = 0;
}
-(void)setData{
_totalEffort.text=[NSString stringWithFormat:@"%d",i];
i = i + 1;
if(i == 100){
[myTimer invalidate];
myTimer = nil;
}
}
这是你的问题代码。希望它能帮助你
- (void)viewDidLoad
{
[super viewDidLoad];
counter = 0;
t = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
-(void)updateLabel
{
for (int i = counter; i<counter+1; i++)
{
_totalEffort.text=[NSString stringWithFormat:@"%d",i];
}
counter++;
if (counter ==101)
{
[t invalidate];
}
}
-(void)viewDidLoad
{
[超级视图下载];
计数器=0;
t=[NSTimer scheduledTimerWithTimeInterval:2目标:自选择器:@selector(updateLabel)userInfo:nil repeats:YES];
}
-(void)updateLabel
{
对于(inti=counter;i这是您的问题代码。希望它能帮助您
- (void)viewDidLoad
{
[super viewDidLoad];
counter = 0;
t = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
-(void)updateLabel
{
for (int i = counter; i<counter+1; i++)
{
_totalEffort.text=[NSString stringWithFormat:@"%d",i];
}
counter++;
if (counter ==101)
{
[t invalidate];
}
}
-(void)viewDidLoad
{
[超级视图下载];
计数器=0;
t=[NSTimer scheduledTimerWithTimeInterval:2目标:自选择器:@selector(updateLabel)userInfo:nil repeats:YES];
}
-(void)updateLabel
{
对于(inti=counter;i您正在用您的循环阻塞运行循环。您只需要在每次迭代中恢复它
for(int i=0;i<100;i++){
_totalEffort.text=@(i).stringValue;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
for(inti=0;i您正在用您的循环阻塞运行循环。您只需要在每次迭代中恢复它
for(int i=0;i<100;i++){
_totalEffort.text=@(i).stringValue;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
for(int i=0;i使用NSTimer并在一段时间间隔后更新标签中的值。for(int i=0;i请参阅使用NSTimer并在一段时间间隔后更新标签中的值。for(int i=0;i请参阅