Ios [ViewController行]:发送到collectionView didSelectItemAtIndexPath中实例的无法识别的选择器
我试图通过segue将集合视图中的选定对象传递给另一个视图控制器。当我单击集合视图中的一个单元格时,应用程序崩溃,并出现以下错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[ViewController行]:未识别的选择器已发送到实例0x136d1fbf0 我是这样做的:Ios [ViewController行]:发送到collectionView didSelectItemAtIndexPath中实例的无法识别的选择器,ios,objective-c,uicollectionview,uistoryboardsegue,Ios,Objective C,Uicollectionview,Uistoryboardsegue,我试图通过segue将集合视图中的选定对象传递给另一个视图控制器。当我单击集合视图中的一个单元格时,应用程序崩溃,并出现以下错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[ViewController行]:未识别的选择器已发送到实例0x136d1fbf0 我是这样做的: @implementation ViewController - (void)viewDidLoad { [super viewDidLoad];
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_friends = [[NSMutableArray alloc] init];
[self refresh];
}
- (void)refresh {
PFQuery *query = [PFQuery queryWithClassName:@"People"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, error.userInfo);
} else {
[self.collectionView reloadData];
}
}];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
NSLog(@"Do nothing");
} else {
[self performSegueWithIdentifier:@"profilePush" sender:self];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FriendsTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsTableViewCell" forIndexPath:indexPath];
cell.friendsDelegate = self;
PFObject *friendObj = [_friends objectAtIndex:indexPath.row];
[(PFFile*)friendObj[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (error) {return;}
cell.profilePic.image = [UIImage imageWithData:data];
}];
cell.nameL.text = friendObj[@"username"];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0) {
return 3;
} else {
return _friends.count;
}
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"profilePush"]) {
NSIndexPath *indexPath = (NSIndexPath*)sender;
ProfilePopupViewController *person = segue.destinationViewController;
person.user = [_friends objectAtIndex:indexPath.row];
//.user is a PFObject in the next view controller
}
}
@end
在
prepareforsgue:sender:
中,sender
不是indepath
。使用indexPathsForSelectedItems
来识别所选单元格错误来自此处
prepareForSegue中的发送方是viewController
您正在将viewController强制转换为NSIndexPath
NSIndexPath *indexPath = (NSIndexPath*)sender;
ProfilePopupViewController *person = segue.destinationViewController;
person.user = [_friends objectAtIndex:indexPath.row];