nsmutablearray不保存值

时间:2015-03-12 19:47:35

标签: objective-c nsmutablearray

我的数组没有保存我输入的值... 我在.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];

但是一旦我到达同一个类中的另一个方法......数组再次为零。我必须为数组做一些愚蠢的事情,不要持有值

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

if (self.boolAddToClient) {

    NSLog(@"add client to list");

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

}
else{

    NSLog(@"startup");

}

}

我正在尝试在另一个类中使用它

- (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");

}

}

1 个答案:

答案 0 :(得分:1)

由于我无法在没有代表的情况下发表评论,我必须尝试回答。

试试这个:

在ViewDidLoad中,使用您在实现中创建的字符串来分配init,并且如果阻止,则更改为:

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

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