如何在 UIAlertView 中显示时间?

How to show time in UIAlertView?

在我的应用程序中,我正在实现录音机。一切正常。

但是当用户点击录制按钮时,我必须以秒为单位显示 UIAlertView

让用户像录音一样容易理解。

我对此一无所知。

我该怎么做,或者请给我建议任何其他想法。

UIAlertView 在 iOS 8 中被弃用,现在您可以使用带有 preferredStyle 警报的 UIAlertController。

如果秒数是静态的你可以使用

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@“Your title string”
                                                               message:[NSString stringWithFormat:@“Seconds: %f”,yourSecondsVariable];
                                                         preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK",nil)
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];

[self presentViewController:alert animated:YES completion:nil];

我认为您可以使用 timeIntervalSinceDate:(属于 NSDate)来初始化您的 yourSecondsVariable

如果您希望时间是动态的(不断更新),那么这应该可以帮助您入门。

@interface ViewController ()
@property (nonatomic, strong) UILabel *timeLabel;
@end

实施:

- (void)timer {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Time" message:@"\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 260, 50)];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        self.timeLabel.text = [NSDate date].description;
    }];

    [alert.view addSubview:self.timeLabel];

    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [timer invalidate];
    }]];

    [self presentViewController:alert animated:YES completion:nil];
}