nsmutable 数组不保存值

nsmutable array not saving values

我的数组没有保存我放入其中的值... 我正在 .h 文件中定义我的 nsmutablearray *arrayClientList

@interface StartupTableViewController :     UIViewController<UITableViewDataSource, UITableViewDelegate>

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

@property NSMutableArray *arrayClientList;
@property BOOL boolAddToClient;

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

@end

在 .m 文件中我是这样初始化的

- (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];

但是一旦我在同一个 class 中使用另一个方法...数组再次为 nil。我一定是在做一些愚蠢的事情让数组不保存值

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

if (self.boolAddToClient) {

    NSLog(@"add client to list");

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

}
else{

    NSLog(@"startup");

}

}

我正在尝试在另一个中使用它 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");

}

}

因为没有代表我不能发表评论,我必须尝试回答。

试试这个:

在 ViewDidLoad 中,使用您在实现中创建的字符串进行分配初始化,并将 if 块更改为:

@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
}

如果不起作用请评论,我也认为你没有正确传递信息。尝试搜索有关如何在 TableViewController 之间传递数组的答案。祝你好运!