我有一個tableView
填充NSString
值從NSFetchedResultsController
。我有配置的單元格檢查/取消選中單元格。這是完美的。將NSManagedObject屬性的字符串值傳遞給新的視圖控制器
我正在嘗試配置單元格,以便單擊單元格內的其中一個標籤可觸發另一個視圖控制器。我配置了標籤,並將日誌語句放在了我想要執行的地方,並且一切正常。當我加入SEGUE,沒有得到編譯器錯誤,但它在運行時崩潰與後續的錯誤:
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'
我相當肯定我做一些令人吃驚的愚蠢,但我難倒什麼。我歡迎提出建議。
MyCustomCell.h:
@property (strong, nonatomic) IBOutlet UILabel *itemDescription;
@property (strong, nonatomic) IBOutlet UILabel *itemGroup;
@property (strong, nonatomic) IBOutlet UIImageView *itemImage;
FirstVC.m方法:
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
方法
- (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;
}
}
SecondViewController.h性能
// The photo is a string that references a filename in my app
@property (nonatomic, strong) NSString *selectedItemPhoto;
@property (nonatomic, strong) NSString *selectedItemTitle;
你在哪裏定義「mySegue」?在你的故事板? –
你會繼續「常規」viewController或TabBarController或NavigationController或沿着這些線? – luk2302
我們正在取得一些進展!我從customCell拖到SecondVC。如果我沒有點擊標籤,它會繼續並檢查我的細胞。我想單擊標籤來執行搜索,但如果我沒有點擊該標籤,我只想檢查/取消選中該單元格。我想我正在試圖做這個錯誤的segue。 – Adrian