我有一个用于创建计时器的标签。我在它下面还有 2 个按钮,最常用的启动和停止。当我按下开始按钮时,会出现一个警报,并且警报中有一个文本字段。一切正常,但是当我单击警报“开始计时器”中的按钮时,我希望标签将我在文本字段中设置的数字作为计时器,然后倒计时。谁能帮我解决这个问题。我搜索了谷歌,甚至多次尝试使用 label.text = [textField.text intValue] 和所有类似的东西。提前致谢
- (NSString*)getTimeStr : (int) time {
seconds = time % 60;
minutes = time / 60;
return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
- (IBAction)startTimer{
// having the user enter a number in seconds
UIAlertView *inputAlert = [[UIAlertView alloc]
initWithTitle:@"Enter The Time:"
message:@"Enter your time in seconds\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Start Timer", nil];
UITextField *inputField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
inputField.backgroundColor = [UIColor whiteColor];
[inputAlert addSubview:inputField];
[inputAlert setTag:1];
[inputAlert show];
NSInteger buttonIndex;
if([inputAlert tag] == 1) {
if(buttonIndex == 1) {
clicked = TRUE;
mainTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(startTimer)
userInfo:nil
repeats:YES];
}
if(buttonIndex == 0) {
clicked = FALSE;
[inputAlert dismissWithClickedButtonIndex:0 animated:YES];
}
}
//NSString *intFromTextField = [inputField text];
//int time = [intFromTextField intValue];
timeLabel.text = [self getTimeStr:(int)inputField.text];
[timeLabel setFont:[UIFont fontWithName:@"Courier New" size:140]];
}