【问题标题】:How can I disable a viewController for a time period on iOS?如何在 iOS 上禁用 viewController 一段时间?
【发布时间】:2014-11-17 14:56:04
【问题描述】:

我想在用户按下 viewController X 上的按钮后的一天内禁用其中一个 viewController X(初始视图)。在此期间,viewController Y 将成为初始视图。我怎样才能正确地实现它?

【问题讨论】:

  • 当用户点击按钮时,将当前时间戳[[NSDate date] timeIntervalSince1970]保存在NSUsetDefault中。每次尝试显示视图控制器 X 时检查此值。如果这是初始视图控制器,则必须选择在 AppDelegate 中以编程方式显示 X 或 Y。或者有一个占位符视图控制器作为初始视图控制器并有条件地呈现 X 或 Y。
  • 如果您只使用 NSDate,您将依赖设备时间。可以由用户更改。如果您想要更高的安全性,您将需要额外的检查并与您的服务器同步。
  • 参考这个。 fantageek.com/481/…。并使用 NSTimer 对象处理您的 viewController。 iCamViewController *icvc = [self.storyboard instantiateViewControllerWithIdentifier:@"iCamViewController"]; icvc.Dispose;

标签: ios


【解决方案1】:

所以在 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;
    }

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多