Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:23:11+00:00 2026-06-14T22:23:11+00:00

I have been struggling with that for some time. I have a tab bar

  • 0

I have been struggling with that for some time. I have a tab bar application. In SecondView I define number2 and save it to NSUserDefaults as I want it to be remembered by the program at the next time the app is open. In the FirstView I want to do some calculation (and display the answer) that include number2 saved earlier. I can’t get it working. In the code, I left only things that are working so far. Any help will be greatly appreciated.

FirstViewController.h

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

@interface FirstViewController : UIViewController {
IBOutlet UILabel *labelAnswer;
}
-(IBAction)buttonCalc;
@property(nonatomic, retain) UILabel *labelAnswer;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize labelAnswer;

-(IBAction)buttonCalc {
    float number1 = 2;

//what should I put here (or somewhere else) to get number2 saved in second view?

    float answer = number1;// + number2
    labelAnswer.text = [[NSString alloc] initWithFormat:@"%f", answer];
}

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController {
    IBOutlet UITextField *textNumber2;
    NSUserDefaults *defaults;
}

-(IBAction)buttonSave;
@property(nonatomic, retain) UITextField *textNumber2;
@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize textNumber2;

-(IBAction)buttonSave {
    //saving my data
    NSString *saveNumber2 = textNumber2.text;
    defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveNumber2 forKey:@"savedNumber2"];
    [defaults synchronize];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //loading my data
    defaults = [NSUserDefaults standardUserDefaults];
    NSString *loadNumber2 = [defaults objectForKey:@"savedNumber2"];
    [textNumber2 setText:loadNumber2];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:23:12+00:00Added an answer on June 14, 2026 at 10:23 pm

    You try following code in your method hope it will help you…

    -(IBAction)buttonCalc {
        float number1 = 2;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *strNumber2 = [defaults objectForKey:@"savedNumber2"];
    
        float number2 = [strNumber2 floatValue];
    
    //what should I put here (or somewhere else) to get number2 saved in second view?
    
        float answer = number1 + number2; 
        labelAnswer.text = [[NSString alloc] initWithFormat:@"%f", answer];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Day, I have been struggling to get this right for quite some time
I have been struggling with this for some time now scanning the net for
I have been struggling with Subversion for some time, thinking it was beginners problems
Really been struggling with this one for some time now, i have many text
I have been struggling with this for quite some time now. jQuery.ajax in the
I am struggling with an SQL time comparison on two fields that have been
I have been struggling with this for some time now. There have been many
Been struggling with this for some time now. Basically I have 6 fields, name,
This is something I have been struggling about for some time now. The thing
I have been struggling with the unholy alliance that is WebSphere 7+ and Mojarra

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.