3

I want to show the "intro" nib i made only at first app launch.

I was using the following code in my viewDidLoad but it seems to do nothing (even in ViewWillAppear). I tried to clean, remove the app from simulator and device and build again but nothing happened.

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
    if (![defaults objectForKey:@"firstRun"]){
        IntroViewController *intro = [[IntroViewController alloc] initWithNibName:nil bundle:nil];

        intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
        [self presentModalViewController:intro animated:YES];

        [intro release];

        [defaults setObject:[NSDate date] forKey:@"firstRun"];

    }
    [[NSUserDefaults standardUserDefaults] synchronize];

I also tried to show a little UIAlertView at first launch, and it works! Am i failing to load the nib?

EDIT

I forgot to say it's a tab Bar based app and i've some code in my app delegate to highlight rows at the first three sessions of the app. Any help appreciated!

Phillip
  • 4,276
  • 7
  • 42
  • 74
  • Possible duplicate of http://stackoverflow.com/questions/2487541/presentmodalviewcontroller-not-working – djromero Oct 29 '11 at 18:47

5 Answers5

3

Ok, add the following code and it should work:

- (void)Loadview_afterdelay{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
    if (![defaults objectForKey:@"firstRun"]){
    IntroViewController *intro = [[IntroViewController alloc] initWithNibName:nil bundle:nil];

    intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
    [self presentModalViewController:intro animated:YES];

    [intro release];

    [defaults setObject:[NSDate date] forKey:@"firstRun"];

      }

   [[NSUserDefaults standardUserDefaults] synchronize];

}


- (void)viewDidLoad {

[self performSelector:@selector(Loadview_afterdelay) withObject:nil afterDelay:0.5];

 }

Apparently, in this case, you can't load a second view right after your view is loaded (viewdidload) Therefor you just set a small delay before loading your second view.

Louis
  • 124
  • 1
  • 7
1

Try the following:

    IntroViewController *intro = [[IntroViewController alloc] initWithNibName:@"IntroViewController" bundle:nil];

    intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
    [self presentModalViewController:intro animated:YES];

    [intro release];

I have added your .nib name to initWithNibName

Also, if you're using iOS 5 (and ARC), you should use this:

    IntroViewController *intro = [[IntroViewController alloc] initWithNibName:@"IntroViewController" bundle:nil];

    intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
    [self presentModalViewController:intro animated:YES completion:nil];
Louis
  • 124
  • 1
  • 7
  • Thanks, i tried the second way because i'm using iOS 5, but still nothing.. Reading the "possible duplicate" question, should i create a UINavigationController to push the view at first launch? – Phillip Oct 30 '11 at 13:52
  • Could you perhaps check if the nib you're trying to load has its Class set? You could check this by going into "Interface builder" (the nib editor), selecting the 'File's Owner' and going to the third tab on the 'right side view' (aka: utilities). The text field 'Class' should be IntroViewController in your case. – Louis Oct 30 '11 at 14:54
  • Yes it has, because when i push it from the button i created it pushes fine. But when i try to show it at the first App launch it doesn't push anything – Phillip Oct 30 '11 at 18:20
  • Is it possible for you to upload your project (or part of it)? – Louis Oct 30 '11 at 18:25
  • Ok, well could you check if in your viewDidLoad you first call [super viewDidLoad] before you run your code? – Louis Oct 30 '11 at 19:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4603/discussion-between-louis-and-phillip) – Louis Oct 30 '11 at 19:23
0

As @Louis already answered the question very well already the only thing is It's just not compatible with the ARC since there are a [ release] What I'll write now isn't new but I'll declare it more for the beginner ones.

First you have to import the TourGuideViewController.h in your RootViewController.h which will be like similarly like this:

#import <UIKit/UIKit.h>
#import "TourGuideViewController.h"

@interface RootViewController : UITabBarController
{
    TourGuideViewController *TourGuideViewController;
}

@property (nonatomic, strong) TourGuideViewController *TourGuideViewController;

@end

Then You have to synthesize and implement your function in your RootViewController.m file:

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize TourGuideViewController = _TourGuideViewController;

- (void)viewDidLoad
{
    [self performSelector:@selector(LoadTourGuide) withObject:nil afterDelay:0.5];
}

- (void)LoadTourGuide{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults objectForKey:@"FirstRun"]){
        TourGuideViewController *TourGuideVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TourGuideViewController"];
        TourGuideVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:TourGuideVC animated:YES completion:NULL];

        [defaults setObject:[NSDate date] forKey:@"FirstRun"];
    }

    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
Ashoor
  • 1,358
  • 1
  • 9
  • 11
  • You should not forget that the **TourGuideViewController** is located in the Storyboard has a **StoryBorad ID** of *TourGuideViewController* and I'm making a call for it from the **RootViewController : UITabBarController** – Ashoor Dec 03 '12 at 07:18
0

Have you tried moving it to viewDidAppear?

toddbranch
  • 82
  • 6
0

It looks to me that you should place that code in your AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Modify the code above to what you need.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[application setStatusBarHidden:YES withAnimation:NO];
return YES;
}

It is there that your telling your application what view to start with. So first time have a firstTimeViewController. If you are doing it here don't animate the transition in. Just animate the transition off.


Put a break point and check if you are going into the viewDidLoad method and if so check if your test is appropriate.
I suggest maybe using a BOOL in an NSNumber to check agains.

On top of that you may consider the option to put that in viewDidAppear.

Vincent Bernier
  • 8,674
  • 4
  • 35
  • 40
  • Uhm seems not working since it's a TabBar based app. I already have some NSUserDefaults code to do a thing for the first 3 sessions of the app. – Phillip Nov 04 '11 at 17:07
  • can you better define the work flow of your application, like when do you want that thing to appear, you want it over your tab controller on in one tab, etc. – Vincent Bernier Nov 04 '11 at 17:22
  • So, my app is made up of a tab bar with 4 tabs. The first one is the welcome one, and in there i made a button which pushes up the "intro" view (with all the instructions). I want to show the intro view as soon as the user starts the app for the first time! – Phillip Nov 04 '11 at 18:15