我是Objective-C的新手。我想在警报视图中显示当前日期和时间。
- (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];
}
答案 0 :(得分:0)
您的警报消息字符串需要重做。试试这个:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Posted"
message:[NSString stringWithFormat:@"Your Message is posted on: %@", todayString]
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
答案 1 :(得分:0)
您可以使用NSString
或stringWithFormat:
联接stringWithString:
,还有其他可用的附加功能,see 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
答案 2 :(得分: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];