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 7832773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:24:25+00:00 2026-06-02T12:24:25+00:00

What I like to do: User enters 10 in UITextField and tap on UIButton

  • 0

What I like to do:

  • User enters 10 in UITextField and tap on UIButton.
  • UILabel shows 10 and UITextField goes blank.
  • User enters 5 in UITextField and tap on UIButton.
  • UILable shows 15 and UITextField goes blank again.

With following code I get the entered number saved and shown in the label but how can I tell the array to add and show me total and not just the very first number I entered?

h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *label;
@property (nonatomic, strong) IBOutlet UITextField *field;

@property (nonatomic, strong) NSString *dataFilePath;
@property (nonatomic, strong) NSString *docsDir;
@property (nonatomic, strong) NSArray *dirPaths;

@property (nonatomic, strong) NSFileManager *fileMgr;
@property (nonatomic, strong) NSMutableArray *array;

- (IBAction)saveNumber:(id)sender;

@end

m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize label, field, dataFilePath, docsDir, fileMgr, dirPaths, array;

- (void)viewDidLoad
{
    [super viewDidLoad];

    fileMgr = [NSFileManager defaultManager];
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    dataFilePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"data.archive"]];

    if ([fileMgr fileExistsAtPath:dataFilePath])
    {
       array = [NSKeyedUnarchiver unarchiveObjectWithFile:dataFilePath];
        self.label.text = [array objectAtIndex:0];
    }
    else 
    {
        array = [[NSMutableArray alloc] init];
    }

}


- (IBAction)saveNumber:(id)sender
{
    [array addObject:self.field.text];
    [NSKeyedArchiver archiveRootObject:array toFile:dataFilePath];
    [field setText:@""];
    [label setText:[array objectAtIndex:0]];
}
  • 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-02T12:24:26+00:00Added an answer on June 2, 2026 at 12:24 pm

    You need to iterate through all the values and add them to a running total. Have a look at this:-

     - (IBAction)saveNumber:(id)sender
    {
        [array addObject:self.field.text];
        [NSKeyedArchiver archiveRootObject:array toFile:dataFilePath];
        [field setText:@""];
    
        // Create an enumerator from the array to easily iterate
        NSEnumerator *e = [array objectEnumerator];
    
        // Create a running total and temp string
        int total = 0;
        NSString stringNumber;
    
        // Enumerate through all elements of the array
        while (stringNumber = [e nextObject]) {
            // Add current number to the running total
            total += [stringNumber intValue];
        }
    
        // Now set the label to the total of all numbers
        [label setText:[NSString stringWithFormat:@"%d",total];
    }
    

    I’ve commented the code for readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a program that goes like this: User enters 3 numbers.
I'm designing an authentication system that works like the following: User enters password Salt
I have an UITextField where the user enters currency value like $1,234.56. In an
When a user enters a search query, I'd like to track: 1) Their search
I would like to make a form in HTML where a user enters some
my app has a timer (like an egg boiling timer) The user enters a
In my iPhone app I'd like to monitor if the user enters some particular
A user enters URLs in a box like this: google.net google.com I then try
I would like to clear Radiobutton list selection after user enters the text in
I'm trying to make a calculator which is basicly like this: user enters about

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.