【问题标题】:Create Action View to Only Be Displayed First Time App is Ran创建仅在第一次运行应用程序时显示的操作视图
【发布时间】:2011-02-04 22:45:44
【问题描述】:

我只是想在第一次打开应用程序时显示一个操作视图,但我不确定如何构建它。

我明白一定要放进去

- (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

我创建了动作视图,但不知道如何让它只在第一次显示,以后再也不显示。

【问题讨论】:

    标签: ios ios4 actionview


    【解决方案1】:

    如果您的应用程序必须记住它之前是否已启动,那么您可以显示或不显示动作视图。您可以做到这一点的一种方法是在应用程序退出时将布尔值保存到文件中,并在应用程序启动时将其读回(如果存在,则应用程序之前已启动)。下面是一些执行类似操作的代码(将其放入您的应用程序委托中)。

    - (void)applicationWillResignActive:(UIApplication *)application { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saved_data.dat"];
    NSMutableData *theData = [NSMutableData data];
    NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];
    BOOL launched = YES;
    [encoder encodeBool:launched forKey:@"launched"];
    [encoder finishEncoding];
    
    [theData writeToFile:path atomically:YES];
    [encoder release];
    }
    

    用于保存,这里是加载代码...

    - (id) init {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saved_data.dat"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]) {
        //open it and read it 
        NSLog(@"data file found. reading into memory");
    
        NSData *theData = [NSData dataWithContentsOfFile:path];
        NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
    
        BOOL launched = [decoder decodeBoolForKey:@"launched"];
    if (launched) {
    //APP HAS LAUNCHED BEFORE SO DON"T SHOW ACTIONVIEW
    }
    
        [decoder finishDecoding];
        [decoder release];  
    } else {
        NSLog(@"no data file found.");
        //APP HAS NEVER LAUNCHED BEFORE...SHOW ACTIONVIEW
    }
    return self;
    }
    

    另请注意,如果您在模拟器中运行,则退出模拟器后此代码将不会执行,您实际上必须像 iPhone 用户那样按下主页按钮。

    【讨论】:

    • 必须写入文件,还是关闭时也能保持布尔值?
    • 它必须以某种方式从启动到启动进行保存(可能不是在进入后台并恢复时,但绝对是在完全关闭时)。保存到文件对我来说似乎是最好的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多