我有一个用于创建计时器的标签。我也有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]];
}
答案 0 :(得分:4)
好的,这里有两件大事:
首先,您应该将alertViewStyle
属性设置为UIAlertViewStylePlainTextInput
,而不是将自己的文本字段添加到警报视图。
第二件事是你没有正确地将你的字符串转换为整数。嗯,你是,但是,由于某种原因,它被注释掉了。你不能只是施展它。如果你这样做,你将得到对象的内存地址,而不是值。您需要在字符串上调用integerValue
。
因此,您将使用以下内容,使用UIAlertViewDelegate
回调并通过textFieldAtIndex:
方法访问文本字段:
- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger) buttonIndex {
// Your existing timer code. It's pretty okay as it is.
NSUInteger enteredInteger = [[alertView textFieldAtIndex:0].text integerValue];
myLabel.text = [self getTimeStr:enteredInteger];
}
答案 1 :(得分:0)
首先,您不能将label.text设置为intValue,因为label.text需要NSString的实例。所以不需要添加.intValue;
您是否将UITextField添加为UIAlertView的子视图?如果是,那么尝试添加一些标记:
[youtTextField setTag:777];
[yourAlertView addSubview:yourTextField]
// you can access yourTextField as a subview with tag = 777
如果您使用UIAlertView,则必须设置其委托: [yourAlertView setDelegate:self]; UIAlertView的委托必须符合UIAlertViewDelegate协议,因此在viewController的.h文件中添加:
@interface yourViewController : UIViewController <UIAlertViewDelegate>
为了符合该协议,您必须在ViewController中实现以下方法:
// this method is called when user taps one of AlertView's buttons
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// as I told, you have to have yourAlertView and timeLabel as class variables
NSString *button=[yourAlertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:@"Start Timer"])
{
NSString * timeValue = ((UITextField*)[yourAlertView viewWithTag:777]).text;
[timeLabel setText:timeValue] // set text for your label
[timeLabel setFont:[UIFont fontWithName:@"Courier New" size:140]];
}
}
另外,不要忘记将timeLabel和alertView设置为类变量:
@interface yourViewController : UIViewController <UIAlertViewDelegate>
{
UIAlertView * yourAlertView;
UILabel * timeLabel;
}
//对startTimer的一些编辑:selector:
- (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 setTag:1];
inputField.backgroundColor = [UIColor whiteColor];
[inputAlert addSubview:inputField];
[inputAlert show];
}