Problem with views and button iPhone application - iphone

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. }

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.

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!

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];
}
}

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

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.

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.