3

First of all excuse my english because I'm french :-)

I'm a iOS dev beginner and I try to use Master Detail view with XCode 4.4.

What I want:

My master view contains UITableView with Prototype cells. When I click on my cell, I want to see the Detail View.

Here is my code:

MasterViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSDictionary* instrument = [Instruments objectAtIndex:indexPath.row];
    NSLog(@"instrument: %@",instrument);

    NSString* status = [instrument objectForKey:@"status"];
    NSLog(@"status: %@",status);

    NSString* imageName = [NSString stringWithFormat:@"%@48.png", [status lowercaseString]];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    imgView.image = [UIImage imageNamed:imageName];
    cell.imageView.image = imgView.image;

    // Set up the cell...
    NSString *cellValue = [instrument objectForKey:@"id"];
    cell.textLabel.text = cellValue;

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"masterToDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        NSDictionary* instrumentDetails = [Instruments objectAtIndex:indexPath.row];

        [[segue destinationViewController] setDetailItem:instrumentDetails];
    }
}

Of course I have a segue in my storyboard linking my prototype cell to the detail view with "masterToDetails" for identifier.

When I click on a prototype cell in my master table, prepareForSegue is not called. Why?

Then when I try to force segue call with:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"masterToDetails" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}

I have the following error: NSInvalidArgumentException, reason: Receiver has no segue with identifier masterToDetails

But it exists in my storyboard!

Maybe the way I use MasterDetail is wrong or maybe it's a stupid error from me...

Tell me if you want other details from my application.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Dorine M
  • 73
  • 1
  • 7

1 Answers1

8

Instead of linking your segue from the cell to the next viewController, drag from the viewController (Small orange icon underneath the View) to the next viewController. Then make sure you give the segue an identifier then use:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"masterToDetails" sender:indexPath];
}

(Copy and paste your segue identifier to alleviate any spelling mistakes).

Darren
  • 10,182
  • 20
  • 95
  • 162
  • Also, check the reuse identifier of your cell in storyboard. According to your code it should be "Cell" – Darren Sep 03 '12 at 21:18
  • FWIW, this concise but detailed answer was worth 100x more than the overly verbose duplicate link at the top of the OP - ty – kfmfe04 Dec 09 '14 at 08:04