作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在用户按下 viewController X 上的按钮后禁用其中一个 viewController X(初始 View )一天。在此期间,viewController Y 将成为初始 View 。我该如何正确实现?
最佳答案
所以在 ViewController X 中你想像这样使用 nsuserdefaults 来保存按下按钮时的日期
// Get the current date
NSDate *now = [NSDate date];
// Save it in nsuserdefaults using the key myDateKet
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"myDateKey"];
在你的 AppDelegate.m 中放这个
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Number of seconds in a day
double dayInSeconds = 86400;
// Get the current date when app opens this gets called
NSDate *today = [NSDate date];
// Get the date saved when user pressed the button
NSDate *savedDate = (NSDate *)[defaults objectForKey:@"myDateKey"];
// If nil then user has never pressed the button
if (savedDate == nil) {
// Therefore view controller x is the root
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"view controller x"];
} else {
// Else user has pressed button so compared the dates
// One day after the saved date
NSDate *oneDayAfterSaved = [NSDate alloc] initWithTimeInterval:dayInSeconds sinceDate:savedDate];
// Compare oneDayAfterSaved to today
NSComparisonResult result = [today compare:oneDayAfterSaved];
// Check if the date is the same has one day after the saved date or after then
if (result == NSOrderedSame || result == NSOrderedDescending) {
// ViewController x is the root
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"view controller x"];
} else {
// else if before one day after saved date then view controller y
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"view controller y"];
}
}
return YES;
}
关于ios - 如何在 iOS 上禁用 viewController 一段时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26975409/
我是一名优秀的程序员,十分优秀!