0

My array is not saving the values I put in it... I am defining my nsmutablearray *arrayClientList in .h file

@interface StartupTableViewController :     UIViewController<UITableViewDataSource, UITableViewDelegate>

  @property (strong, nonatomic) IBOutlet UITableView *tableView;

@property NSMutableArray *arrayClientList;
@property BOOL boolAddToClient;

//@property (strong, nonatomic) NSMutableArray *arrayAddClient;

@end

in .m file I am initializing like so

- (void)viewDidLoad {
[super viewDidLoad];

//initialize variables
self.arrayClientList = [[NSMutableArray alloc] init];
arraySelectedInformation = [[NSMutableArray alloc] init];
self.boolAddToClient = NO;

NSString *tstring = @"hello";
[self.arrayClientList addObject:tstring];

but then once I get to another method in this same class... the array is nil again. I must be doing something stupid for the array not to hold the values

-(void)viewDidAppear:(BOOL)animated{
//NSLog(@"appeared");

if (self.boolAddToClient) {

    NSLog(@"add client to list");

    self.boolAddToClient = NO;
    [self.tableView reloadData];

}
else{

    NSLog(@"startup");

}

}

I am trying to use it in another class

- (IBAction)buttonSubmit:(id)sender {

NSString *userDescription = [[NSString alloc] init];
NSString *userUsername = [[NSString alloc] init];
NSString *userPassword = [[NSString alloc] init];

userDescription = self.textfieldDescription.text;
userUsername = self.textfieldUserID.text;
userPassword = self.textfieldPW.text;

//check to make sure user filled out all fields
if (![userDescription isEqual:@""] && ![userUsername isEqual:@""] && ![userPassword isEqual: @""]){

    NSLog(@"correct");

    NSArray *arrayVC = self.navigationController.viewControllers;
    StartupTableViewController *parentViewController = [arrayVC objectAtIndex:0];
    parentViewController.boolAddToClient = YES;

    NSMutableArray *arrayNewObjects = [[NSMutableArray alloc] initWithObjects:userDescription, userUsername, userPassword, nil];

    NSMutableArray *tarray = parentViewController.arrayClientList;
    [tarray addObject:arrayNewObjects];
    [parentViewController.arrayClientList addObject:arrayNewObjects];

    [self.navigationController popViewControllerAnimated:YES];

}
else{

    NSLog(@"something missing");

}

}

2
  • You don't show where you're using the array again or how it's called... Commented Mar 12, 2015 at 20:05
  • i added the code for the other class I am trying to access it in. it give me the NIL. even when I am in the original class, in another method, looking at the debugger area... its nil in there as well Commented Mar 12, 2015 at 20:54

1 Answer 1

1

Since I can't comment without rep, I must try with answer.

Try this:

In ViewDidLoad do alloc init with Strings you create in implementation and also change if block to this:

@implementation 
{
    NSString *userDescription;
    NSString *userUsername;
    NSString *userPassword;
}
-(void)viewDidLoad {
    [super viewDidLoad];
    NSString *userDescription = [[NSString alloc] init];
    NSString *userUsername = [[NSString alloc] init];
    NSString *userPassword = [[NSString alloc] init];
}
- (IBAction)buttonSubmit:(id)sender {
if (self.textfieldDescription.text.lenght != 0 && self.textfieldUserID.text.lenght != 0 && self.textfieldPW.text.lenght != 0) {
userDescription = self.textfieldDescription.text;
userUsername = self.textfieldUserID.text;
userPassword = self.textfieldPW.text;
....... and the rest
}

Please comment if it's not working, and I also think that you're not passing the informations right. Try searching an answer on how to pass arrays between TableViewControllers. Good Luck!

Sign up to request clarification or add additional context in comments.

6 Comments

I've tried it and the array is still NIL... something I am doing is making my original array from the .h file back to NIL
please edit @property NSMutableArray *arrayClientList; to @property (strong, nonatomic) NSMutableArray *arrayClientList; also, did you tried to search for an answer on how to pass arrays between TableViewControllers? If passing doesn't do the trick, try writing that array to .plist file (by code) and also read array from .plist file. That MUST do the trick. It can't be nil. Nil is only when it's not allocated and initialized.
ughhh. so I think the problem is else where in my code I reset the array and didn't even notice. I also made the change you mentioned to strong and nonatomic. I have been reading passing between tableviewcontroller.... I thought i was doing one of the options to do this the right way.. no? either way it is working now. thanks!
Glad I could help anyhow. Good luck!
one last thing... why did you say to put the 3 strings in the implementation, then in viewdidload alloc/init... instead of all in the same method like I had?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.