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
Since you allocate your numberList in the
ViewDidLoadmethod, be sure to call yourinsertNewNumbermethod after the call toViewDidLoad.I believe that
works, right?
If you need to call your
insertNewNumbermethod before the call toViewDidLoad, allocate your numberListin an overloaded
initWithNibName:bundle:method.