0

我怎样才能创建一个模态视图,也许是从委托来检查它是否是第一次启动应用程序并显示一个带有关闭按钮的模态视图,以便通知用户一些重要的事情?

我需要该视图仅在应用程序第一次启动时显示,并且不再显示。

4

4 回答 4

3

首先:在这里查看我的早期答案:https ://stackoverflow.com/a/13563490/1359306

这是一个密码保护模式视图,我每次打开应用程序时都会使用它。答案应该有助于解决实施解决方案时可能出现的一些问题。它performSegueWithIdentifier就像我使用故事板一样使用,但您也可以presentViewController:animated:completion:用来展示您的视图。

然后:请注意,有一个if声明可以用来检查是否需要呈现视图。就我而言,我检查用户是否离开应用程序超过 5 分钟。我通过在NSUserDefaults每次applicationWillResignActive调用时设置一个日期来做到这一点 - 并检查该日期与当前日期/时间之间的差异)。

在您的情况下,您可以执行以下操作:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"firsttime"] == nil) {
    //display modal view
    [defaults setObject:@"YES" forKey:@"firsttime"];
    [defaults synchronize];
}

这将检查NSUserDefaults应用程序的。如果 'firsttime' 为 nil(即首次下载应用程序时),我们将显示视图。然后我们将键设置为“YES”,这意味着它永远不会再等于 nil - 所以视图永远不会显示。

这对于在应用首次加载时显示说明很有用。或者,您可以存储日期或数字,以使代码在未来更具适应性。

的文档NSUserDefaults可以在这里找到。

于 2013-02-19T16:14:16.490 回答
1

您可以使用NSUserDefaults.

在 delegate.h 文件中

NSString * firstTime;

在 .m 文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *myStr = [defaults objectForKey:@"str"];
    firstTime =myStr;

    if (firstTime.length==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hi" message:@"You have open this app first time" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
        [alert show];
        [self firstTime];
    }

    return YES;
}

-(void)firstTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *myStr = @"first";
    [defaults setObject:myStr forKey:@"str"];
    [defaults synchronize];
}
于 2013-02-19T16:13:43.877 回答
1

在您的AppDelegate使用方法中didFinishLaunchingWithOptions检查您的应用程序是第一次启动还是第二次像这样启动

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstTime"])
    {
        // show your main view controller
    }
    else
    { // your app is launching first time ....

      // show your modalview controller here and dismiss it & go to main view controller


      // don't forget to update the key so that next time user don't hang in else statement...

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstTime"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
}
于 2013-02-19T16:15:05.267 回答
0

You can use the presentViewController:animated:completion: in the UIViewController class.

So here is what I'd do:

  1. Load your main view controller
  2. Check your user preferences to see if your "firsttime" key exists (in your view did load most likely)
  3. If it doesn't display your one-time view
  4. When it completes (which you can know by having a delegate call back to your main view controller), save the "firsttime" key to your user preferences

no source, code (my Mac is at home), but if you follow these simple steps it should work fine for you.

于 2013-02-19T16:06:19.783 回答