所以我想访问并在我的函数外部显示格式化的日期。对于日期格式,我使用NSDateFormatter
,它工作正常。NSMutableString访问问题
我的功能(didFinishUpdatesSuccessfully
)执行某些操作,如果成功则显示包含格式化日期的UIAlertView
。所有这一切工作正常..
- (void) didFinishUpdatesSuccessfully {
//--- Create formatted date
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY - hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:currDate]; // dateString contains the current date as a string
[dateFormatter release];
//--- UIAlertView
NSString *title = @"The update has been performed!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
message: dateString
delegate: nil
cancelButtonTitle: [FileUtils appResourceForKey:@"UPDATE_GENERAL_BUTTON_TITLE_OK"]
otherButtonTitles: nil];
[alert show];
[alert release];
//--- create new string
// NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];
}
我现在想的dateString
值写入全球NSString
或NSMutableString
和访问其他地方的代码,例如其他功能等。
我想过创造一个NSMutableString
这样的: NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];
和访问lastUpdated
别的地方,但ouside这个功能lastUpdated
是空的......你能帮忙吗?干杯
感谢您的回复。那么,我真的不喜欢使用全局变量,我同意对此做出的评论......我想要的是保留并显示'lastUpdate'值(在UIAlertView之前的最后一个成功操作的日期触发currentDate(现在),所以用户可以决定不更新,如果它没有意义...我想我可以在'didFinishUpdatesSuccessfully'中添加一个带有标签的子视图。但这里的问题是,标签将被堆叠在一起,具有新的价值......这有意义吗?任何其他想法? – Jan
很难告诉你如何重新组织你的代码,而不需要了解更多信息,比如'-didFinishUpdatesSuccessfully'的存在位置。由于它听起来像一个委托方法,所以我猜测它在视图控制器中。所以,我想你可能想要存储控制器可以访问的最后一次更新的日期(可能是NSUserDefaults)。然后让视图控制器获取日期,将其转换为字符串,并将现有标签的值设置为该字符串。视图控制器负责视图,所以它应该知道如何找到标签。 – Caleb
谢谢Caleb,你猜对了。它是视图控制器中的代表。将值存储在NSUserDefaults中似乎是合理的。我现在必须弄清楚如何做到这一点。任何snippert可用?谢谢 – Jan