【问题标题】:nsmutable array not saving valuesnsmutablearray 不保存值
【发布时间】:2015-03-12 19:47:34
【问题描述】:

我的数组没有保存我放入其中的值... 我在 .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");

}

}

【问题讨论】:

  • 你不会再次显示你在哪里使用数组或者它是如何被调用的......
  • 我为我试图访问它的另一个类添加了代码。它给了我 NIL。即使我在原来的班级,在另一种方法中,查看调试器区域......它在那里也是 nil

标签: objective-c nsmutablearray


【解决方案1】:

由于没有代表我无法发表评论,因此我必须尝试回答。

试试这个:

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

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

【讨论】:

  • 我已经尝试过了,但数组仍然是 NIL……我正在做的是将原始数组从 .h 文件恢复为 NIL
  • 请将@property NSMutableArray *arrayClientList; 编辑为@property (strong, nonatomic) NSMutableArray *arrayClientList;,您是否尝试搜索有关如何在TableViewControllers 之间传递数组的答案?如果传递不起作用,请尝试将该数组写入 .plist 文件(通过代码),并从 .plist 文件中读取数组。那必须解决问题。不能为零。 Nil 仅在未分配和初始化时才出现。
  • 呃。所以我认为问题出在我的代码中我重置数组的其他地方,甚至没有注意到。我还将您提到的更改为强和非原子。我一直在阅读 tableviewcontroller 之间的传递....我以为我正在做一个正确的方法..不是吗?无论哪种方式,它现在都在工作。谢谢!
  • 很高兴我能帮上忙。祝你好运!
  • 最后一件事......为什么你说要把 3 个字符串放在实现中,然后放在 viewdidload alloc/init ......而不是像我一样用同样的方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多