2016-09-29 1 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を使用できるようになりました。

秒はあなたが私はあなたがあなたのyourSecondsVariableを初期化する(NSDateの)timeIntervalSinceDate:を使用することができると思います

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

を使用することができる静ある場合。

+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]; 
} 
関連する問題