我有一个tableView
来自NSString
的{{1}}值。我将单元配置为检查/取消选中单元格。这非常有效。
我正在尝试配置单元格,因此单击单元格中的一个标签会触发到另一个视图控制器的segue。我配置了标签并在我想要做segue的地方扔了一个日志声明,一切正常。当我添加segue时,没有编译器错误,但它在运行时崩溃并出现以下错误:
NSFetchedResultsController
我相当肯定我做了一件令人吃惊的事情,但是我很难过。我欢迎提出建议。
MyCustomCell.h:
2015-05-31 08:09:04.656 MyApp[17682:1240919] -[UIViewController selectedItemPhoto:]: unrecognized selector sent to instance 0x7fa42b65d4e0
2015-05-31 08:09:04.694 MyApp[17682:1240919] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController selectedItemPhoto:]: unrecognized selector sent to instance 0x7fa42b65d4e0'
FirstVC.m方法:
@property (strong, nonatomic) IBOutlet UILabel *itemDescription;
@property (strong, nonatomic) IBOutlet UILabel *itemGroup;
@property (strong, nonatomic) IBOutlet UIImageView *itemImage;
方法
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// implement custom cell
MyCustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
// Get a hold of myManagedObject from FRC
MyNSManagedObject *myObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Configures 1st label to look like a hyperlink
customCell.itemDescription.text = myObject.itemDescription;
customCell.itemDescription.textColor = [UIColor blueColor];
customCell.itemDescription.userInteractionEnabled = YES;
// Enables gesture and sets demoObject to pass along via the segue called from labelTap
UITapGestureRecognizer *labelTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTap)];
[customCell.itemDescription addGestureRecognizer:labelTapGesture];
self.demoObject = myObject;
// Configures 2nd label within customCell
customCell.itemGroup.text = myObject.itemGroup;
// add image to cell, already imported into Images.xcassets
NSString *imageName = [NSString stringWithFormat:@"%@-1.jpg", myObject.photo];
customCell.imageView.image = [UIImage imageNamed:imageName];
// The following code ensures random checkmarks don't appear when the user scrolls.
if ([self.selectedObjects containsObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]) {
customCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
customCell.accessoryType = UITableViewCellAccessoryNone;
}
return customCell;
}
方法
didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Set the checkmark accessory for the selected row.
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone) {
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[self.selectedObjects addObject:self.selectedObject];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} else {
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
[self.selectedObjects removeObject:self.selectedObject];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// Enables save button if there are items in the selectedObjects array
if (self.selectedObjects.count > 0) {
[self.saveButton setEnabled:YES];
} else {
[self.saveButton setEnabled:NO];
}
}
方法
labelTap
- (void) labelTap {
// The "hyperlink" effect I'm trying to achieve works without the segue
NSLog(@"itemDescription tapped");
[self performSegueWithIdentifier:@"mySegue" sender:nil];
}
方法
prepareForSegue
SecondViewController.h属性
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"mySegue"] && self.demoObject != nil) {
// This line returns a value...
NSLog(@"self.demoObject = %@", self.demoObject.itemDescription);
// ...but it crashes here when it tries to set on the destinationViewController
SecondViewController *destinationViewController = [segue destinationViewController];
destinationViewController.selectedItemPhoto = self.demoObject.photo;
destinationViewController.selectedItemTitle = self.demoObject.itemDescription;
}
}
答案 0 :(得分:1)
从错误看,您似乎没有设置第二个视图控制器的类。它似乎是UIViewController
,应该是SecondViewController
。
在故事板中选择第二个视图控制器并将其类设置为SecondViewController
。
另一个建议是,在Objective C中,你应该在投射对象之前使用内省。所以在你的代码中我会添加:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"mySegue"] && self.demoObject != nil) {
// This line returns a value...
NSLog(@"self.demoObject = %@", self.demoObject.itemDescription);
// ...but it crashes here when it tries to set on the destinationViewController
if ([[segue destinationViewController] isKindOfClass:[SecondViewController class]]) {
SecondViewController *destinationViewController = (SecondViewController *)[segue destinationViewController];
destinationViewController.selectedItemPhoto = self.demoObject.photo;
destinationViewController.selectedItemTitle = self.demoObject.itemDescription;
}
}
}
通过这种方式,segue不会发生,但应用程序不会崩溃。
答案 1 :(得分:0)
我明白了!我有三个问题(两个被捆绑在一起):
问题1: Juan Catalan指出我没有设置SecondViewController
的故事板
问题2: Yuchen& luk2302提出了我如何执行segue的问题。
问题3:我不需要prepareForSegue
这就是我的所作所为:
1:我删除了prepareForSegue
。
2:我删除了故事板segue。
2:我更改了labelTap
方法,为labelTapGesture
添加参数
- (void) labelTap:(UITapGestureRecognizer *)sender {
NSLog(@"stretchDescription tapped");
CGPoint location = [sender locationInView:self.view];
if (CGRectContainsPoint([self.view convertRect:self.tableView.frame fromView:self.tableView.superview], location)) {
CGPoint locationInTableview = [self.tableView convertPoint:location fromCoordinateSpace:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:locationInTableview];
if (indexPath) {
self.demoObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
DisplayStretchViewController *destinationViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"];
// Set properties on destinationVC
destinationViewController.itemPhoto = self.demoObject.photo;
destinationViewController.itemTitle = self.demoObject.stretchDescription;
[self.navigationController pushViewController:destinationViewController animated:YES];
}
}
}
我还发现这篇文章非常有帮助: UILabel with Gesture Recognizer inside UITableViewCell blocks didSelectRowAtIndexPath
感谢大家的投入!