Reputation: 11
I'm trying to make my app launch a different view on the first time it is loaded up. I've got this code at the moment which implements that something should happen when the app is first launched. I've got this code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasRunBefore = [defaults boolForKey:@"FirstRun"];
if (!hasRunBefore) {
[defaults setBool:YES forKey:@"FirstRun"];
[defaults synchronize];
// what goes here??
else
{
NSLog (@"Not the first time this controller has been loaded");
So I should launch a different view controller in the if statement. But what should I put ?
Upvotes: 0
Views: 967
Reputation: 1349
You're not really providing enough information to properly answer the question. The answer could depend on:
For the sake of argument, let's say you are not using storyboards and you've somehow constructed a view controller in your app delegate.
You could check your first run status there and do as Prasad suggested: (Assuming you refactor your first run check into a separate method in app delegate) ...in didFinishLaunchingWithOptions:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* controller;
if([self isFirstRun]) {
controller = [[MyFirstRunController alloc] init];
} else {
controller = [[MyStandardController alloc] init];
}
[[self window] setRootViewController:controller];
[self.window makeKeyAndVisible];
....
That's one way. Another option, UIViewController provides a loadView method specifically for creating your controllers' view. So another approach would be to just create your standard view controller in app delegate and then do your first run check inside that controllers loadView override. And then set the appropriate view there.
It really depends on what your first run view does and whether it needs its own controller or can be managed by your standard controller. Only you know that.
If you go this latter route, you would do like this your standard controller:
-(void)loadView {
UIView *rootView;
CGRect frame = [[UIScreen mainScreen] bounds];
if([self isFirstRun]) {
rootView = [[MyFirstRunView alloc] initWithFrame:frame];
} else {
rootView = [[MyStandardView alloc] initWithFrame:frame];
}
[self setView:rootView];
}
Update
If loading from a storyboard, your best bet would be to keep your 'default' controller as is and check your first run status before the view loads, perhaps in viewWillAppear - and then load your storyboard based view controller manually from the storyboard and present in modally. Something like:
- (void)presentFirstRunViewController {
UIStoryboard *storyboard = self.storyboard;
FirstRunViewController *controller = [storyboard instantiateControllerWithIdentifier:@"FirstRunController"];
// Configure the new controller
[self presentViewController:controller animated:YES completion:nil];
}
In general, the first approach (two controllers) is preferred because its a cleaner separation of responsibility. But it really depends on your needs.
Please confirm the code - typed on a Windows machine (vs pasted from XCode)...
Upvotes: 4