2013-02-22 138 views
0

我是Objective-C的新手。我想在警報視圖中顯示當前的日期和時間。UIAlertView帶時間戳

- (IBAction)postButtonPressed:(UIButton *)sender { 

    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"MMM d, yyyy h:mm" options:0 locale:[NSLocale currentLocale]]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:formatString]; 

    NSString *todayString = [dateFormatter stringFromDate:[NSDate date]]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Posted" message:@"Your Message is posted on: @todayString" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
    [alert show]; 
     } 
+1

什麼是你的問題? – 2013-02-22 05:43:58

回答

0

您的警報消息字符串需要重做。試試這個:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Posted" 
message:[NSString stringWithFormat:@"Your Message is posted on: %@", todayString] 
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
0

可以使用stringWithFormat:stringWithString:有還提供其他appeding功能concate NSStringsee doc

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Posted" message:[NSString stringWithFormat:@"Your Message is posted on: %@",todayString] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
[alert show]; 
[alert release]; //don't forget to release UIAlertView object if you don't using ARC 
0

嘗試

NSString *str = [NSString stringWithFormat:@"Your Message is posted on: %@", todayString]; 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Posted" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
[alert show];