2015-05-31 52 views
0

我有一個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; 
+1

你在哪裏定義「mySegue」?在你的故事板? –

+1

你會繼續「常規」viewController或TabBarController或NavigationController或沿着這些線? – luk2302

+0

我們正在取得一些進展!我從customCell拖到SecondVC。如果我沒有點擊標籤,它會繼續並檢查我的細胞。我想單擊標籤來執行搜索,但如果我沒有點擊該標籤,我只想檢查/取消選中該單元格。我想我正在試圖做這個錯誤的segue。 – Adrian

回答

0

我想通了!我有三個問題,(兩個人綁在一起):

問題1:胡安加泰羅尼亞指出,我沒有我的故事板爲SecondViewController

問題設置2:雨辰& luk2302提出的問題我如何表演賽格。

問題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

感謝所有您的輸入!

1

從看來你沒有設置班級的第二種觀點的錯誤控制LER。它似乎是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不會發生,但應用程序將不會崩潰。

+0

謝謝!你是我的一個問題,但潛在的問題依然存在。基本上,如果標籤被點擊,我想繼續,但如果沒有點擊,請選中/取消選中。 – Adrian

+0

使用'UIButton'而不是'UILabel',你將能夠從按鈕中定義一個segue。這應該可以解決你的問題。 –

相關問題