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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:06:22+00:00 2026-06-12T04:06:22+00:00

Goal of program: Enter numbers on a viewController. When the user hits Submit button,

  • 0

Goal of program: Enter numbers on a viewController. When the user hits Submit button, the data entered by user is passed on to a different class for displaying on a different viewController.

Problem: I am trying to access an instance variable (numberList) in an instance method (-(void)insertNewNumber:(Numbers *)tempNumber), but it never gives me the correct output. But when I access the same variable through a protocol method of UITableViewDataSource, I get the correct answer. I figured this by using NSLog in instance method and protocol method.

Since I have declared numberList as a property variable, I was thought that I can access it from anywhere in the program and get the correct value stored in it. But compiler returned 0 for the NSLog statements when they were called from instance method. When the NSLog statements from protocol method, showed the correct result.

Please help me understand why is this occurring and how can I add elements into an array from any method in a program.

Thank you!

Here’s the relevant code I am working on:

Numbers.h:

@interface Numbers:NSObject

@property (strong, retain) NSString *ID;
@property (strong, retain) NSInteger *number;

@end

Numbers.m

@implementation Numbers

@synthesize ID, number;

@end

DisplayNumbers.h

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

@interface DisplayNumbers : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *numberList;

- (void)insertNewNumber:(Numbers *)tempNumber;

@end

DisplayNumbers.m:

@implementation DisplayNumbers

@synthesize numberList;


- (void)viewDidLoad
{
    [super viewDidLoad];

    numberList = [[NSMutableArray alloc] init]; 

    Numbers *num0 = [[Numbers alloc] init];
    Numbers *num1 = [[Numbers alloc] init];

    num0.ID = [NSString stringWithFormat:@"ID 0"];
    num0.number = 1111111111;

    num1.ID = [NSString stringWithFormat:@"ID 1"];
    num0.number = 2222222222;


    [numberList addObject:num0];    
    [numberList addObject:num1];    

}

- (void)insertNewNumber:(Numbers *)tempNumber
{
    NSLog(@"numberList.count (in -(void)insetNewNumber) = %d", numberList.count);
    [numberList addObject:tempNumber];
    NSLog(@"numberList.count (in -(void)insetNewNumber) = %d", numberList.count);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberList.count (in -(NSInteger)tableView:...) = %d", numberList.count);            
    Numbers *temp = [[Numbers alloc] init];
    temp.ID = @"hi";
    temp.Number = 1234;

    [numberList addObject:temp];

    NSLog(@"numberList.count (in -(NSInteger)tableView:...) = %d", numberList.count);                
   return numberList.count;
}

@end

Edit 1: Calling of insertNewNumber:.
This method is being called from a different class.

InputNumber.h:

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

@interface InputNumber:UIViewController

@property (retain, strong) NSInteger *enteredNumber;
-(void)enteredNumber;

@end

InputNumber.m

@implementation InputNumber

@synthesize enteredNumber;
-(void)enterNumber
{
    DisplayNumber *temp = [[DisplayNumber alloc] init];
    [temp insertNewNumber:enteredNumber];
}
@end   
  • 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-12T04:06:23+00:00Added an answer on June 12, 2026 at 4:06 am

    Since you allocate your numberList in the ViewDidLoad method, be sure to call your insertNewNumber method after the call to ViewDidLoad.

    I believe that

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        Numbers *temp = [[Numbers alloc] init];
        temp.ID = @"hi";
        temp.Number = 1234;
        [self insertNewNumber:temp];
       return contactList.count;
    }
    

    works, right?

    If you need to call your insertNewNumber method before the call to ViewDidLoad, allocate your numberList

    numberList = [[NSMutableArray alloc] init]; 
    

    in an overloaded initWithNibName:bundle: method.

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

Sidebar

Related Questions

Goal: Once i click on the start button on my user interface, i currently
The goal is to create a program which will effectively let the user create
Goal: When a user click a exit button in the file > exit or
Goal: Don't want the user to make changes in the program's size. Problem: Don't
My Goal I am attempting to make a Java program in which a user
The goal is to have the user select a java program, then my program
My final goal is to write the program which can run on the Unix
Hmmm. Consider this program, whose goal is to figure out the best way to
I wrote a C++ program with the goal of quickly inserting an element into
The goal is to periodically read files from a folder to which another program

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.