Obj-C全球使用NSArray

时间:2013-03-16 14:32:06

标签: iphone ios objective-c xcode

我有一个ViewController,提示FBFriendPickerViewController,我在选择时返回一个包含选择的NSArray。现在我想使用这个选择信息提示并显示一个新的ViewController。我是Objective C的新手,但我想解决方案非常简单。这是我的建议:

ViewController2.h

- (id)initWithStyle:(UITableViewStyle)style andSelection:(NSArray *)selection;
@property (strong, nonatomic) NSArray *selectedParticipants;

ViewController2.m

- (id)initWithStyle:(UITableViewStyle)style andSelection:(NSArray *)selection {
    self = [super initWithStyle:style];
    if (self) {
        self.title = NSLocalizedString(@"Split Bill", nil);
        self.tableView.backgroundColor = [UIColor wuffBackgroundColor];
        self.selectedParticipants = selection;
    }
    return self;
}

- (void)setSelectedParticipants:(NSArray *)selectedParticipants {
    NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%d rowsInSection", [self.selectedParticipants count]);
    return [self.selectedParticipants count];
}

ViewController1.m

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 2) {
        [[self friendPickerController] presentModallyFromViewController:self animated:YES handler:^(FBViewController *sender, BOOL donePressed) {
            if (donePressed) {
                ViewController2 *vc = [[ViewController2 alloc] initWithStyle:UITableViewStyleGrouped
                                                                                andSelection:[self.friendPickerController selection]];
                [self.navigationController pushViewController:vc animated:YES];
            }
            //[[self friendPickerController] clearSelection];
            }
         ];
    }
}

然而,似乎第一个setSelectedParticipants-log返回正确数量的所选朋友,但numberOfRowsInSection-log返回0.

为什么会这样?

提前致谢!

2 个答案:

答案 0 :(得分:2)

问题在于你的二传手:

- (void)setSelectedParticipants:(NSArray *)selectedParticipants {
    NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]);
}

您会注意到,您从未实际设置支持该属性的实例变量的值,在这种情况下,默认值为_selectedParticipants。因此,要修复,只需将以下行添加到您的setter:

_selectedParticipants = selectedParticipants;

你应该好好去。

答案 1 :(得分:0)

从代码中删除此功能

- (void)setSelectedParticipants:(NSArray *)selectedParticipants {
    NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]);
}

您已经在init方法中设置了selectedParticipants