Creating a initial / first boot viewcontroller with NSUserDefaults in storybord - iphone

So im trying to skip my first viewcontroller if a NSUserDefaults key is found. But its not working. Is it smart to use a Segue for this? Im new to storybord.
Warning: Attempt to present on whose view is not in the window hierarchy!
Im using this code in the viewDidLoad of the first VC:
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
[self performSegueWithIdentifier:#"SegueToNewTrackTraceVC" sender:self];
}else{
NSLog(#"First launch");
}
NSLog(#"initialViewController loaded");
}

You forgot to update the NSUserDefault after the first launch.
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
[self performSegueWithIdentifier:#"SegueToNewTrackTraceVC" sender:self];
}
else
{
NSLog(#"First launch");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
}

You can't perform a segue from viewDidLoad, because the controller whose class that code is in, is about to be presented itself, and its view has not been added to the window hierarchy yet. You should put the code in viewDidAppearAnimated:. IF you don't want to see that segue happening, then uncheck the "Animates" box for the segue in the storyboard (that box is available for modal transitions, I don't think it is for a push).

Related

tableView didSelectRow not properly functioning

My other question was ambiguous so I will make this one clearer. I have a table view on one viewController and when a cell is selected, it saves a number to NSUserDefaults. On my main viewController, I retrieve this number and want to update a label with it. I did some debugging and found that when I go back to my main viewController from the tableView one, this is the order at what happens:
1) main view loads
2) tableView cell tap recognized
3) number saved to NSUserDefaults
How would I change the order so that the main view loads last so that when I click a cell, it changes the label in my main viewController?
Here is the code from my didSelectRow method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *pointsPath = [[NSString alloc] initWithFormat:#"totalPoints"];
NSString *numberFirst = [[NSString alloc] initWithFormat:#"numberFirst"];
int totalPoints = [[NSUserDefaults standardUserDefaults] integerForKey: pointsPath];
int number = [[NSUserDefaults standardUserDefaults] integerForKey:numberFirst];
if([indexPath row] == 1)
{
NSLog(#"Selected First row");
if(totalPoints < 10)
{
if (number != 0)
{
NSLog(#"Did not add a point, but saved");
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:numberFirst];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else if (number == 0)
{
NSLog(#"Added a Point");
int newPoints = totalPoints + 1;
[[NSUserDefaults standardUserDefaults] setInteger:newPoints forKey: pointsPath];
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:numberFirst];
[[NSUserDefaults standardUserDefaults] synchronize];
int logPoints = [[NSUserDefaults standardUserDefaults]integerForKey:pointsPath];
NSLog(#"Point Count:%i", logPoints);
}
}
A controller's view is loaded on demand. So the thing that triggers viewDidLoad is somebody else performing controller.view. If you've already pushed the view controller into a container view or otherwise started to present it then that'll be the agent responsible.
viewDidLoad, and under iOS 5 and below viewDidUnload, are intended to give you the hooks you need if you want to create some part of the view programmatically.
If what you're doing is trying to account for a setting that may have been changed while the controller wasn't visible, you should probably use viewWillAppear:. That's the intended way to determine when you're about to transition from invisible to visible.
If you wanted a completely active link between the two things then you would set up the view controller that displays the number to listen for NSUserDefaultsDidChangeNotification and to update views as necessary when that occurs.
Guys i finally found the problem if anyone has this problem... You need to make sure that when the user selects a cell, it is a push segue not a modal... This is so frustrating that is was so simple!!

Switching start up view controller Xcode

Say for example if I have two buttons one for apples and one for orange, and I select apples and takes me to the apples screen. How can I make it for now on every time I run the app it will go to the apples screen?
in viewDidLoad
if([[NSUserDefaults standardUserDefaults] objectForKey:#"fruit"] != nil)
{
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"fruit"]isEqualToString:#"apple"]) {
[self.navigationController pushViewController:appleVC animated:NO];
}
else{
[self.navigationController pushViewController:orangeVC animated:NO];
}
}
and on Button Methods
on Apple button
[[NSUserDefaults standardUserDefaults] setObject:#"apple" forKey:#"fruit"];
on Orange button
[[NSUserDefaults standardUserDefaults] setObject:#"orange" forKey:#"fruit"];
You can store information like this using NSUserDefaults.
You'd store a boolean bAppleSelected like this:
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setBool:bAppleSelected forKey=#"appleSelected"];
You can read it by accessing the default userDefaults:
BOOL bApple = [standardUserDefaults boolForKey=#"appleSelected"];
On your app delegate you must have some method that instantiates the very first controller and displays it in a window. You can just create an "apples controller" and push it there
You can use NSUserdefaults here,
NSString* fruit=#"apple";
[[NSUserDefaults standardUserDefaults]setObject:fruit forKey:#"controllerName"];
[[NSUserDefaults standardUserDefaults]synchronize];
and insted of the name string of your firstview controller in appdelegate file use the above NSUserDefaults.

Problems displaying view controller when local notification received on iPhone

The following code is my latest attempt to display a view controller when my application receives a particular local notification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(#"Notification Received. UserInfo: %#", [notification.userInfo valueForKey:#"notificationType"]);
if ([[notification.userInfo valueForKey:#"notificationType"] isEqualToString:#"backup"])
{
NSLog(#"Backup notification received.");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"Yes" forKey:#"shouldBackup"];
[defaults synchronize];
SettingsViewController *vc = [self.window.rootViewController.tabBarController.viewControllers objectAtIndex:3];
[self.window.rootViewController.tabBarController.navigationController pushViewController:vc animated:NO];
}
else
{
NSLog(#"Did receive notification: %#, set for date:%# .", notification.alertBody, notification.fireDate);
}
}
I then have this piece of code which I use in the viewDidAppear method to determine wether or not to perform the backup:
if ([[defaults objectForKey:#"shouldBackup"] isEqualToString:#"Yes"]){
[defaults setObject:#"No" forKey:#"shouldBackup"];
[defaults synchronize];
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Backing Up Your Data..";
HUD.minSize = CGSizeMake(135.f, 135.f);
[HUD showWhileExecuting:#selector(performBackup) onTarget:self withObject:nil animated:YES];
}
However, it appears that viewDidAppear is never called, can anyone explain what it is I'm doing wrong? I've tried several different approaches now and I just can't seem to get it to work..
Thanks,
Tysin
You need to push the SettingsViewController onto the stack from a NavigationController in order for its delegate methods to be called (in this case viewDidAppear). It seems that your root view controller is a TabBarController. In this case add the UINavigationControllerDelegate to your root VC, more info on what to do next can be found here http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca
Also, are you sure viewDidAppear is not getting called or just the code within it is not getting called? Add a breakpoint to find out.
Lastly, do you haveĀ [super viewDidAppear:animated]; in the viewDidAppear method of SettingsViewController?
If all else fails you can always call viewDidAppear manually from the parent view controller :)
Hope this helps!

Login Screen with Storyboarding possible?

I am playing around with the new iOS 5 features and trying to rewriting one of my apps as pure iOS 5 app using the new storyboarding feature.
To cut a long story short, I have a start screen where the app tries to connect to a server if the user saved some login data, if not, it should ask for them.
Here is how I would do it. I create a Viewcontroller which is doing the connection thing in the viewDidLoad method. If there is no login data or the login is not successful, I need a to do a manual segue to the login screen.
Now is this even possible, or do I need 2 story boards for that ?
I have solved it by putting a login view without any segues (to or from it) like in the screenshot below:
Then, I used a custom class in the tab bar controller to show it whenever I need it.
In the tab bar controller class, I use 'viewDidLoad' to fire up the login view. To show the modal view, I do have a singleton thingy that stores some state, say BOOL isAuthenticated, where I do the magic:
- (void) performLoginIfRequired: (UIViewController *) source {
if (!self.isAuthenticated) {
NSLog(#"Is not authed");
UIStoryboard *storyboard = [UIApplication sharedApplication].delegate.window.rootViewController.storyboard;
UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:#"loginScreen"];
[source presentModalViewController:loginController animated:YES];
} else {
NSLog(#"Is authe");
}
}
And, in my case, I wanted it to be shown when the app first starts, but also when it enters foreground again. So, I registered my tab bar controller with the notification center, so I get notified if the app is coming back:
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
In the willEnterForeground method, I do:
-(void) willEnterForeground: (NSNotification *)notification {
[[myStateThingy defaultState] performLoginIfRequired:self];
}
It sounds like you need to use the performSegueWithIdentifier method. Make sure both views are in the same storyboard, link them together using a Push segue, and give that segue a name. Then, from your first view controller's code simply call the performSegueWithIdentifier to perform a manual segue.
Hope this helps!
See also: Conditionally following a segue
Cheers,
Jesse L. Zamora
I had this same issue, and I solved it simply by doing the following: Instead of trying to segue to a login screen(modally or push), I made the login screen my root view controller. In the login view controller's viewWillAppear method, I check if someone's logged in already. If so, I push my home screen:
// mutableFetchResults is an array with my persistent Credentials object
if ([mutableFetchResults count] > 0) { // Someone's already logged in
[self performSegueWithIdentifier:#"Home" sender:self];
}
Also, in the Home screen view controller's viewWillAppear method, I hid the back button with this line, so the user can't go "back" to the login screen:
self.navigationItem.hidesBackButton = YES;
Finally, every page of my app has a "Sign Out" bar button on the top right. Signing out and putting the login screen up was as simple as this:
- (IBAction)signOutButtonPressed:(UIBarButtonItem *)sender {
MyAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate signOutCurrentUser]; // this method in my app delegate deletes the current Credentials
[self.navigationController popToRootViewControllerAnimated:YES];
}
Hope that was simple enough!
After trying many different methods, I was able to solve this problem with this:
-(void)viewWillAppear:(BOOL)animated {
// Check if user is already logged in
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([[prefs objectForKey:#"log"] intValue] == 1) {
self.view.hidden = YES;
}
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// Check if user is already logged in
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([[prefs objectForKey:#"log"] intValue] == 1) {
[self performSegueWithIdentifier:#"homeSeg3" sender:self];
}
}
-(void)viewDidUnload {
self.view.hidden = NO;
}

Do we always needs to dismissModelViewController that we had presented?

I have Edited whole Question
What is the difference between presenting the ModelView Controller and dismissing the modal view controller in following situation??
HomeView
TableItemSelection View
PickerView
In HomeView
I am writing code in viewWillAppear to fetch values from NSUserDefault that is set from TableView. Below is my code for viewDidLoad (for initial values) and viewWillAppear(when new value from tableview)
- (void)viewDidLoad
{
[super viewDidLoad];
actionMinutes = #"0";
actionSeconds = #"0";
restMinutes = #"0";
restSeconds = #"0";
}
- (void) viewWillAppear:(BOOL)animated
{
actionMin = [actionMinutes intValue];
actionSec = [actionSeconds intValue];
restMin = [restMinutes intValue];
restSec = [restSeconds intValue];
NSLog(#"acMin:%d acSec:%d reMin:%d reSec:%d",actionMin,actionSec,restMin,restSec);
}
In TableItemSelection View
I am presenting this view from HomeView. Now i want to set value of NSString in HomeView based on Table's didSelectRowAtIndex Method. I am using NSUserDefault to set the value.
And with Done Button touch i am presenting HomeView. (Actually I have to dismissModalViewController) But when i use dismiss I am not able to get values in NSString of HomeView. I am getting values of the table from PickerView. (I am instructed to do that). Below is my code for Table view on DONE button touch
HomeView *homeView = [[HomeView alloc] init];
[homeView.view setBackgroundColor:[UIColor clearColor]];
[homeView.view setFrame:CGRectMake(0 ,40, 320, 460)];
homeView.actionMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:#"actionMinute"];
homeView.actionSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:#"actionSecond"];
homeView.restMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:#"restMinute"];
homeView.restSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:#"restSecond"];
homeView.song = [[NSUserDefaults standardUserDefaults] valueForKey:#"songstring"];
NSLog(#"%#",homeView.actionMinutes);
[self presentModalViewController:homeView animated:YES];
//[self dismissModalViewControllerAnimated:YES]; // if this method is used then no values are passed to HomeView
[homeView release];
In PickerView
I am fetching the values from pickerview and then store it in UserDefault.
below is my code for pickerview
NSUserDefaults *actionTime = [NSUserDefaults standardUserDefaults];
[actionTime setValue:min forKey:#"actionMinute"];
[actionTime setValue:sec forKey:#"actionSecond"];
So why exactly i am not able to get UserDefault values when dismissing ModelView.??? Does presenting a new view everytime will make a stack of views???
#DShah: Yes most definitely..!!
You need to dismiss every modal view.
Also Please post the code you are using.
Also keep in mind that you need to put the NSUserDefaults line first (the line which you use assign NSString value to NSUserDefaults) and then put the line where in you dismissModalViewControllerAnimated:.
If you require more help please leave me a comment.
Hope this helps you.
I think you are trying to store integer value in NSUserDefaults then you have to
Use setInteger:forKey: instead of setObject:forKey:. An integer is not an object, it's a primitive type.
To retrieve it use integerForKey:.
This is the final answer through which i am able to solve my problem
Here is a code on Done button touch event...
- (IBAction) btnDonePressed:(id)sender {
aurioTouchAppDelegate *appDelegate = (aurioTouchAppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.homeView.actionMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:#"actionMinute"];
appDelegate.homeView.actionSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:#"actionSecond"];
appDelegate.homeView.restMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:#"restMinute"];
appDelegate.homeView.restSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:#"restSecond"];
// [self presentModalViewController:homeView animated:YES];
[self dismissModalViewControllerAnimated:YES]; // if this method is used then no values are passed to HomeView
}