- (void) say: (id)formatstring,...
{
va_list arglist;
va_start(arglist, formatstring);
id statement = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
va_end(arglist);
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:statement message:nil delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil] autorelease];
[av show];
[statement release];
}
-(void) action: (UIBarButtonItem *) item
{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
formatter.dateFormat = @"MM/dd/YY HH:mm:ss";
NSString *timestamp = [formatter stringFromDate:[NSDate date]];
[self say:@"At the chime, the time will be %@", timestamp];
}
{
va_list arglist;
va_start(arglist, formatstring);
id statement = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
va_end(arglist);
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:statement message:nil delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil] autorelease];
[av show];
[statement release];
}
-(void) action: (UIBarButtonItem *) item
{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
formatter.dateFormat = @"MM/dd/YY HH:mm:ss";
NSString *timestamp = [formatter stringFromDate:[NSDate date]];
[self say:@"At the chime, the time will be %@", timestamp];
}
本文介绍了一个简单的iOS应用中用于显示当前时间的方法。通过使用NSDateFormatter格式化日期并弹出UIAlertView展示时间信息。该方法利用Objective-C的特性,通过可变参数列表实现灵活的时间格式输出。
2万+

被折叠的 条评论
为什么被折叠?



