【问题标题】:Global NSDate variable seems to make app crash全局 NSDate 变量似乎使应用程序崩溃
【发布时间】: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


    【解决方案1】:

    在我使用变量 dateString 的方法中,

    我用过:

    if([dateString length] == 0){
     //perform standard procedure 
    }
    else {
       //what I want it to do 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2016-12-30
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      相关资源
      最近更新 更多