0

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

ViewController2.h

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

视图控制器2.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。

为什么是这样?

提前致谢!

4

2 回答 2

2

这里的问题在于你的二传手:

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

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

_selectedParticipants = selectedParticipants;

你应该很高兴。

于 2013-03-16T14:37:03.377 回答
0

从您的代码中删除此功能

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

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

于 2013-03-16T14:35:38.400 回答