Iphone Obj-C全局使用NSArray
我有一个ViewController,它会提示FBFriendPickerViewController,在其中,我在选择时返回一个包含该选择的NSArray。现在,我想使用此选择信息提示并显示一个新的ViewController。我不熟悉目标C,但我想解决方法很简单。以下是我的建议: ViewController2.hIphone Obj-C全局使用NSArray,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,我有一个ViewController,它会提示FBFriendPickerViewController,在其中,我在选择时返回一个包含该选择的NSArray。现在,我想使用此选择信息提示并显示一个新的ViewController。我不熟悉目标C,但我想解决方法很简单。以下是我的建议: ViewController2.h - (id)initWithStyle:(UITableViewStyle)style andSelection:(NSArray *)selection; @property
- (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日志似乎返回正确数量的选定好友,但numberOfRowsInSection日志返回0
为什么会这样
提前谢谢 从代码中删除此函数
- (void)setSelectedParticipants:(NSArray *)selectedParticipants {
NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]);
}
您已经在init方法中设置了selectedParticipants这里的问题在于您的设置程序:
- (void)setSelectedParticipants:(NSArray *)selectedParticipants {
NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]);
}
_selectedParticipants = selectedParticipants;
您会注意到,您从未实际设置支持该属性的实例变量的值,在本例中,默认值是\u selectedParticipants
。因此,要修复此问题,只需在setter中添加以下行:
- (void)setSelectedParticipants:(NSArray *)selectedParticipants {
NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]);
}
_selectedParticipants = selectedParticipants;
而且你应该准备好出发。你从来没有在你的setter中设置过任何东西!没有人说他应该“删除”它——只需要对它进行修复,以实际设置基础变量。我建议的是一个简单的修复方法。正如我在文章中提到的“他已经在他的init函数中设置了数组”,这是有道理的。起初,我尝试(在setSelectedParticipants内部)执行一个
self.selectedParticipants=selectedParticipants
,这导致了一个循环,所以我认为这是一种回调函数。但由于我已经将我的财产合成为_selectedParticipants,这是有意义的。从PHP到Obj-C的转换可能需要一些时间:)谢谢@CasparAleksanderBangJespers没问题,很乐意帮忙:)