NSuserdefaults not loading in a tab bar controller, works on uiview - iphone

I have a problem getting the NSUserDefaults to pull out on a tab/nav/tableview controller page, I've put the same code on a loginpage and the app will pull the objects for keys perfectly. I had this working when I had programmatically created the tabs and navs but now it's not working. The loginViewController is not in the tab stack, is that the reason it works?
-(void)refreshFields {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
usernameLabel.text = [defaults objectForKey:kUsernameKey];
passwordLabel.text = [defaults objectForKey:kPasswordKey];
}
- (void)viewDidAppear:(BOOL)animated {
[self refreshFields];
[super viewDidAppear:animated];
if ([usernameLabel.text length] == 0|| [passwordLabel.text length] == 0)
{
UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[loginViewController release];
[self.navigationController presentModalViewController:loginNavigationController animated:NO];
[loginNavigationController release];
}
else
{
[[self tableView ]reloadData];
}
}

Your usernameLabel and passwordLabel outlets aren't set. Verify by putting a breakpoint in refreshFields.

Related

Objective c. Login user using storyboard

I have an storyboard with three view controllers, the init view with a button Init, the login view controller and the list view controller. When I click in the button init in the first view controller, I would like to verify whether the user logged in in order to switch to the login view or to the list view. How could I implement this using segues (segue conditionals??)
You could do something like this
BOOL isLoggedIn = [[NSUserDefaults standardUserDefaults] boolForKey:#"isLoggedIn"];
NSString *headingStoryboardID = isLoggedIn ? #"YourAlreadyLoggedInVC_ID" : #"YourLoginVC_ID";
if([headingStoryboardID isEqualToString:#"YourAlreadyLoggedInVC_ID"]) {
AlreadyLoggedInClass *vc1 = (AlreadyLoggedInClass *)[self.storyboard instantiateViewControllerWithIdentifier:#"YourAlreadyLoggedInVC_ID"];
[self presentViewController:vc2 animated:YES completion:nil];
} else {
LoginViewController *vc2 = (LoginViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"YourLoginVC_ID"];
[self presentViewController:vc2 animated:YES completion:nil];
}
OBS: Ugly and uncompiled code but hope the concept gets across.
EDIT DUE TO COMMENT
To perform a push segue you call
[self.navigationController pushViewController:vc1 animated:YES];
instead.
You can Store your login value in user default when user login like this
in LOginViewController
-(void) doLogin
{
NSString * str = #"Loged In";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:str forKey:#"login"];
[defaults synchronize];
}
And in first view controller check whether this NSdefault value is nil or not.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userLogIn = [defaults objectForKey:#"login"];
if (userLogIn.length !=0) {
//then user Loged in
}else
{
//then user not Loged in
}
This code is just example you can change it acording to your need.

How to Redirect to home tab after login Iphone App?

In my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//Get uer ID from user defaults
NSString *userid = [defaults objectForKey:#"UserId"];
if([userid isEqualToString:#""]){
login = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
[window addSubview:login.view];
} else {
[window addSubview:[rootTabBarController view]];
}
[self.window makeKeyAndVisible];
return YES;
}
and after login success i have this code
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:serverOutput forKey:#"UserId"];
//show tabbar app
NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate];
[appsDelegate.window addSubview:[appsDelegate.rootTabBarController view]];
so the question is how can i redirect to specific tab [Home tab FOR EXAMBLE]?
[appDelegate.rootTabBarController setSelectedIndex:GiveIndexOfTab]; // give index of home tab for ex. 0
If you want to jump to a specific tab without intercepting the tabBar, rather than the first default tab
use
[appsDelegate.rootTabBarController setSelectedIndex:2]; // 2 is a pseudo index i have assigned
For tabbar controller,
SelectedIndex propery helps you to manage its behavior.
Use
setSelectedIndex:Index
method. It'll solve your purpose.
Thanks.

Problem with views and button iPhone application

I have this scenario, I have 4 views. v1,v2,v3,v4.
I am in view v1, which is table view when i press any cell it takes me to another view v2. Fine.
Then in v2 i have some textFields and button B2. When i press button B2(i have set in its method to save textField data and pushViewController function) it takes me to v3.
I have a button in v3 as well B3. This button saves data on v3 and takes me to view 4 i.e. v4.
In v4 all textfields of v2 and v3 is shown.
I have no problem. But when i press navigation back barbutton it takes me to v3 from v4. but now when i change textField value and press button on v3 again it does nothing.same as if press navigation bar button again i.e. back button it takes me v2 and button B2 does nothing.
Can anyone help me regarding this.
-(IBAction)buttonPress:(id)sender{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:field1.text forKey:#"field1Key"];
[defaults setObject:field2.text forKey:#"field2Key" ];
[defaults synchronize];
if (co==nil) {
co=[[class4 alloc]initWithNibName:nil bundle:nil];
co.name=field1.text; co.phone=field2.text;
[self.navigationController pushViewController:co animated:YES ];
[co release];
} –
}
You are pushing ViewControllers inside the if (co ==nil) block. During the first navigation, the view controller is actually nil and so it worked. If you navigate back and press the button again, it will not work - it makes sense !
You should push view Controllers whether is it nil or not. You can do like this.
if (co==nil) {
co=[[class4 alloc]initWithNibName:nil bundle:nil];
}
co.name=field1.text; co.phone=field2.text;
[self.navigationController pushViewController:co animated:YES ];
[co release];
or make co=nil before checking for it.
co = nil;
if (co==nil) {
co=[[class4 alloc]initWithNibName:nil bundle:nil];
co.name=field1.text; co.phone=field2.text;
[self.navigationController pushViewController:co animated:YES ];
[co release];
} –
//do like this to all viewcontrollers action
-(IBAction)buttonPress:(id)sender{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:field1.text forKey:#"field1Key"];
[defaults setObject:field2.text forKey:#"field2Key" ];
[defaults synchronize];
if (co==nil) {
co=[[class4 alloc]initWithNibName:#"class4" bundle:nil];
co.name=field1.text;
co.phone=field2.text;
  [self.navigationController pushViewController:co animated:YES ];//updated
[co release];
}
else {
[self.navigationController pushViewController:co animated:YES ];
[co release];
}
}
//note this line co=[[class4 alloc]initWithNibName:#"class4" bundle:nil];
I found the answer to my question.
-(IBAction)buttonPress:(id)sender{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; [defaults
setObject:field1.text forKey:#"field1Key"]; [defaults setObject:field2.text
forKey:#"field2Key" ]; [defaults synchronize]; if (co==nil) { co=[[class4
alloc]initWithNibName:nil bundle:nil]; co.name=field1.text; co.phone=field2.text;
[self.navigationController pushViewController:co animated:YES ];
[self.navigationController pushViewController:co animated:YES ];[co release]; } co=nil;
//This point to be noted. }

Can I call presentModalViewController in viewDidLoad..?

In my iPhone app I have HomeViewController and ContentViewController. I am saving the values in ContentViewController by using NSUserDefaults
and based on saved values I will load the ContentView instead of HomeView when the app is restarted. if there r no values in the NSUserDefaults it displays the HomeView.
in HomeView I have some buttons..it's like this.. each button is for a book so in contentView all the page nos (in the bottom in a scroll view in ContentView) will be displayed if I click on a page no it displays the text in the above label of ContentView.if the user closes the app in contentView, the page no and book no will be saved...if the user clicks on home button all the information will be deleted.
In the Homeview im checking the NSUserDefaults, if it contains values it should display that exact page of that book
the following is the code...
//HomeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
contentViewController = [[ContentViewController alloc] initWithNibName:#"ContentView" bundle:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"...%d,%d,%d",[prefs integerForKey:#"Stage"],[prefs integerForKey:#"Stimulus"],[prefs integerForKey:#"Card"]);
if(!([prefs integerForKey:#"Stage"] ==0 && [prefs integerForKey:#"Stimulus"] ==0 && [prefs integerForKey:#"Card"] ==0)){
[contentViewController setCurrentState:[prefs integerForKey:#"Stage"]];
[contentViewController setCurrentStimulus:[prefs integerForKey:#"Stimulus"]];
[contentViewController setCurrentCard:[prefs integerForKey:#"Card"]];
[self presentModalViewController:contentViewController animated:YES];
}
}
but it's displaying the homeview.
Try using the method viewDidAppear shown below instead of viewDidLoad
- (void)viewDidAppear:(BOOL)animated
{
contentViewController = [[ContentViewController alloc] initWithNibName:#"ContentView" bundle:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"...%d,%d,%d",[prefs integerForKey:#"Stage"],[prefs integerForKey:#"Stimulus"], [prefs integerForKey:#"Card"]);
if(!([prefs integerForKey:#"Stage"] ==0 && [prefs integerForKey:#"Stimulus"] ==0 && [prefs integerForKey:#"Card"] ==0))
{
[contentViewController setCurrentState:[prefs integerForKey:#"Stage"]];
[contentViewController setCurrentStimulus:[prefs integerForKey:#"Stimulus"]];
[contentViewController setCurrentCard:[prefs integerForKey:#"Card"]];
[self presentModalViewController:contentViewController animated:YES];
}
}

Load an other View from applicationDidFinishLaunching in the AppDelegate

I have this code in my applicationDidFinishLaunching in the AppDelegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firsttime = [defaults stringForKey:#"firsttime"];
if (firsttime == nil) {
BenutzerdatenViewController *Benutzer = [[BenutzerdatenViewController alloc] initWithNibName:nil bundle:nil];
Benutzer.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[Benutzer release];
[defaults setObject:#"lasttime" forKey:#"firsttime"];}
else { [window addSubview:viewController.view];
[window makeKeyAndVisible];}
Always when I open the app the first time I just see a with "view" instead of the right view with buttons and so on. Where is the problem?
You need to add the actual view to your window. For firsttime, you need the following:
[window addSubview: Benutzer.view];
Also, don't release that viewController; you should store a reference to it somewhere.
In the other case (!firstime), it's unclear where you're getting viewController from; I assume it's a member variable.