0

我有一个用于创建计时器的标签。我在它下面还有 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]];

}

4

2 回答 2

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];
}
于 2013-04-13T20:01:41.503 回答
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: 选择器进行一些编辑:

- (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];
}
于 2013-04-13T20:02:30.163 回答