gpt4 book ai didi

ios - 如何在 iOS 上禁用 viewController 一段时间?

转载 作者:行者123 更新时间:2023-11-29 02:30:17 25 4
gpt4 key购买 nike

我想在用户按下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com