2016-09-29 36 views
0

在我的應用程序中,我正在實施錄音機。一切工作正常。如何在UIAlertView中顯示時間?

但是當用戶點擊錄製按鈕時,我必須以秒爲單位顯示UIAlertView

因此,用戶可以很容易地理解像錄音。

我對此沒有任何想法。

我該怎麼做,或者建議我任何其他想法請。

+0

時需要不斷的移動,是嗎?或者只是一個靜態的時間? – prabodhprakash

+0

可能的重複:http://stackoverflow.com/questions/10236167/updating-uialertview-message-dynamically-and-newline-character-issue – prabodhprakash

回答

2

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]; 

我認爲,你可以使用(的NSDatetimeIntervalSinceDate:來初始化你yourSecondsVariable

+1

加1用於'UIAlertView在iOS 8中已棄用' –

+0

我沒有看到這是如何回答OP的問題的。這只是如何呈現警報控制器的標準片段。這個問題明確指出「如何顯示*時間*」 - 時間是動態的,並改變所有*時間*。 – norders

1

如果你想要時間是動態的(不斷更新),那麼這應該讓你開始。

@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]; 
}