まゆたまガジェット開発逆引き辞典

電子工作やプログラミングのHowtoを逆引き形式で掲載しています。作りたいモノを決めて学んでいくスタイル。プログラマではないので、コードの汚さはお許しを。参照していないものに関しては、コピペ改変まったく問いません

Tabbarでアプリ初回起動時にヘルプ画面(ImageView)を表示

アプリを初めて起動させたときに操作方法などヘルプ画面を表示させたいときがあると思います。そんなとき使うのがNSUserDefaults。
NSUserDefaultsと言えば、本来は一時的にデータを保存して次回起動させたときにロードするというやり方が一般的です。
が、今回はそれを応用して以下の方法でアプリを初めて起動させたかどうか判定しています。
Tabbar+マルチタスクはいろいろと面倒なようで、これで正解かどうか確証はないです・・・すみません。
サンプルはこちらです。GitHub - prince9/HelpViewer: A help is displayed when starting an application for the first time. アプリを初めて起動させるとき、Tabbarで吹き出し型のヘルプを表示します。
Tab1とTab2は吹き出しのImageViewが違うだけで後は同じです。

1.アプリケーションを終了させるときに「1」という数値を保存する
2.「1」という数値が保存されていないとき=初回起動時に吹き出し型のヘルプ画面を表示(ImageView)
3.吹き出しはタップすれば消える
4.「1」という数値が保存されているとき=次回起動時からはヘルプは表示されない

・AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
    //追加、アプリを終了するときに「1」という数を保存
    NSInteger saveint;

}

@property (strong, nonatomic) UIWindow *window;
//追加
@property (nonatomic,assign) NSInteger saveint;

@end

・AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate
//追加
@synthesize saveint;
@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //追加、Intsave(saveint)の数を呼び出す
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    saveint = [defaults integerForKey:@"Intsave"];
    
    return YES;
}
...
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //追加、Intsaveに「1」という数を保存する、"1" is saved at Intsave. 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:1 forKey:@"Intsave"];
    [defaults synchronize];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
 //追加、Intsave(saveint)の数を呼び出す
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    saveint = [defaults integerForKey:@"Intsave"];
    NSLog(@"load2 = %d",saveint);
}
...
- (void)applicationWillTerminate:(UIApplication *)application
{
      //追加、Intsaveに「1」という数を保存する
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:1 forKey:@"Intsave"];
    [defaults synchronize];
}

・FirstViewController.h

@interface FirstViewController : UIViewController
//保存している数値を表示する
@property (weak, nonatomic) IBOutlet UILabel *countV;
//吹き出し(ヘルプ表示)の画像
@property (weak, nonatomic) IBOutlet UIImageView *hukidashi1;

//アプリが初回の起動かどうか判定
- (void)FukiHantei;

//タップして吹き出しを消す
- (void)hideFukidashi;

@end

・FirstViewController.m

#import "FirstViewController.h"
//追加、Add
#import "AppDelegate.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize countV;
@synthesize hukidashi1;

//追加、- (void)FukiHantei:を実行
- (void)viewWillAppear:(BOOL)animated
{
    [self FukiHantei];
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //追加
    //タップすると吹き出しを消す指令を出す
    hukidashi1.userInteractionEnabled = YES;
    [hukidashi1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideFukidashi)]];
}
...
//追加
//アプリの初回起動時のみ吹き出し(ヘルプ画面)を表示 
- (void)FukiHantei {
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    countV.text = [NSString stringWithFormat:@"%d",appDelegate.saveint];
    if (appDelegate.saveint == 0) {
        hukidashi1.image = [UIImage imageNamed:@"Fukidashi1.png"];
        
    } else {
        hukidashi1.image = nil;
    }
    
}

//追加、Add
//吹き出しを消す
- (void)hideFukidashi {
    hukidashi1.image = nil;
}