【问题标题】:Passing NSMutableArray to other class returns empty array将 NSMutableArray 传递给其他类返回空数组
【发布时间】:2014-07-25 12:44:51
【问题描述】:

我已经阅读了有关此问题的其他答案,起初我遇到了(null)错误,但现在已修复。现在,每当我记录我在“SelectTeacherTableViewController.h”中创建并传递给 SendMessageViewController 类的 NSMutableArray 时,它都会创建一个数组,但它是空的。

这是我的 SelectTeacherTableViewController.h 中的代码

#import <UIKit/UIKit.h>

@interface SelectTeacherTableViewController :
UITableViewController
{
    NSMutableArray *recipients;
}

@property (strong, nonatomic) NSArray *teachers;
@property (strong, nonatomic) NSMutableArray *recipients;

@end

这是来自我的 SelectTeacherTableViewController 的代码,我在其中合成收件人并通过方法向他们添加对象。 (代码缩短)

#import "ParseStarterProjectAppDelegate.h"
#import "SelectTeacherTableViewController.h"
#import <Parse/Parse.h>

@interface SelectTeacherTableViewController ()

@end

@implementation SelectTeacherTableViewController
@synthesize recipients;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Message";
    self.recipients = [[NSMutableArray alloc] init];
    [PFUser logInWithUsername:@"awesome" password:@"password"];
    NSString *getCurrentUserSchoolKey = [[PFUser currentUser] objectForKey:@"schoolKey"];
    NSString *currentUserSchoolKey = [NSString stringWithFormat:@"%@", getCurrentUserSchoolKey];
    PFQuery *queryForTeachers = [PFUser query];
    [queryForTeachers orderByAscending: @"username"];
    [queryForTeachers whereKey: @"role" equalTo:@"Teacher"];
    [queryForTeachers whereKey:@"schoolKey" equalTo:currentUserSchoolKey];
    [queryForTeachers findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"%@ %@", error, [error userInfo]);
    }
    else {
        self.teachers = objects;
        NSLog(currentUserSchoolKey);
        [self.tableView reloadData];
    }
}];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.recipients = [[NSMutableArray alloc] init];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    PFUser *user = [self.teachers objectAtIndex:indexPath.row];

if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [recipients addObject:user.objectId];
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [recipients removeObject:user.objectId];
}

NSLog(@"%@", recipients);
}

当我在此处记录收件人时,他们确实包含所有正确的值。但是,这是我的 SendMessageViewController,我从 SelectTeacherTableViewController 传递数组:

SendMessageViewController.m(代码缩短):

#import "SendMessageViewController.h"
#import <Parse/Parse.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import "SelectTeacherTableViewController.h"

@interface SendMessageViewController ()
@end

@implementation SendMessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    SelectTeacherTableViewController *recipientList = [[SelectTeacherTableViewController alloc] init];
    NSMutableArray *recipients = [[NSMutableArray alloc] initWithArray:recipientList.recipients];
    NSLog(@"%@", recipientList);
}

在这里,当我记录收件人时,数组为空。

我已经阅读了一些内容并遇到了一些解决方案,其中提到了一些关于创建简单文件或使用 AppDelegate 存储全局变量的内容,但这些解决方案似乎都不适合我。我有一种感觉,当我继续时,数据正在丢失。我的代码有什么地方出错了吗?我对 Objective-C 有点陌生,这个问题已经困扰了我好几个小时。谢谢。

如果您需要查看更多代码,请询问我。谢谢。

【问题讨论】:

  • 以下两个答案都是正确的,您只需创建一个新的控制器实例。请看这个stackoverflow.com/questions/9907684/…你的问题有一些答案。
  • 那么我应该使用 prepareForSegue 通过 segue 传递数据吗?
  • 啊!太感谢了!终于做到了!
  • 您不传入数据,而是将引用传递给数据。我希望您能理解其中的细微差别。

标签: ios objective-c arrays


【解决方案1】:

这:SelectTeacherTableViewController *recipientList = [[SelectTeacherTableViewController alloc] init]; 创建一个新对象。您添加到其他 SelectTeacherTableViewController 内的 recipients 数组中的任何内容都无关紧要。

如果您需要使用已添加到原始对象的信息,则必须传递对该对象的引用……而不是创建新对象。

【讨论】:

    【解决方案2】:

    [[SelectTeacherTableViewController alloc] init] 不会为您提供对现有收件人列表的引用,它会创建一个新的。在面向对象编程中,您可以拥有同一个类的多个实例,但它们不是同一个对象。

    类!=对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2011-11-29
      • 1970-01-01
      • 2017-01-14
      • 2017-05-23
      • 2021-08-15
      • 2013-06-03
      相关资源
      最近更新 更多