gpt4 book ai didi

iphone - 用相同的方法满足特定条件后如何使计时器无效?

转载 作者:行者123 更新时间:2023-12-01 18:28:03 25 4
gpt4 key购买 nike

嗨,我正在尝试在我的iPhone应用程序中显示自定义进度条,因为我正在编写一种增加进度条值的方法,一旦其值变为100%,则我需要使计时器无效,并且需要停止此递归并显示下一个viewController。
我的代码段如下所示,

-(void)progressNextValue
{
progressValue += 1.0f;
if(progressValue >= progress.maxValue){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"end" message:@"TimeOut!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
NSLog(@"Time Out!!!!");
[mytimer invalidate];
Second *sec = [[Second alloc] initWithNibName:@"Second" bundle:nil];
[self.view addSubview:sec.view];
}

progress.currentValue = progressValue;
mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];

}

- (void)viewDidLoad
{
[super viewDidLoad];
progress.maxValue = 100.0f;
[self progressNextValue];
}

即使我的 progressValue = progress.maxValuemytimer没有在这里失效。

任何帮助都将受到赞赏。

最佳答案

每次运行该方法时,您都在设置计时器。添加一个return语句,或将计时器实例化放置在else语句中。

例如:

-(void)progressNextValue
{
progressValue += 1.0f;
if(progressValue >= progress.maxValue){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"end" message:@"TimeOut!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
NSLog(@"Time Out!!!!");
[mytimer invalidate];
Second *sec = [[Second alloc] initWithNibName:@"Second" bundle:nil];
[self.view addSubview:sec.view];
} else {
// Move this line inside the else statements so that it only gets run if
// the progress bar is not full.
mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];
}

progress.currentValue = progressValue;
}

关于iphone - 用相同的方法满足特定条件后如何使计时器无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11433478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com