Reputation: 1017
I want to disable one of the viewController X(initial View) for a day after the user has pressed a button on viewController X. viewController Y will become initial view during this duration. How can I implement this properly?
Upvotes: 0
Views: 64
Reputation: 796
So in ViewController X you want to use nsuserdefaults to save the date when the button is press like this
// Get the current date
NSDate *now = [NSDate date];
// Save it in nsuserdefaults using the key myDateKet
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"myDateKey"];
In your AppDelegate.m put this
- (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;
}
Upvotes: 1