【发布时间】:2014-04-28 06:46:12
【问题描述】:
我在 FirstViewController.h 中有一个全局变量
extern NSString *dateString;
它记录了我按下 saveText 按钮的日期/时间。还有另一个名为 readText 的按钮会推送 UITableView。此表的第一个单元格将打印保存的日期。
但是,这只有在我首先按下 saveText 按钮时才有效。否则,如果我只按 readText,它就会崩溃。我怎样才能得到它,以便在没有保存当前日期时间的情况下不会崩溃?
这是我为 saveButton 编写时间戳的地方:
-(IBAction)saveText:(id)sender{
//code to save text, irrelevant to the question//
dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle];
NSLog(@"%@",dateString);
}
这里是加载带有时间戳的tableviewcells的代码
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str = @"Text- saved on: ";
str = [str stringByAppendingString: dateString];
self.list = [[NSArray alloc] initWithObjects: str, nil];
}
【问题讨论】:
标签: uitableview nsstring nsdate nsdateformatter