objective c - click table row and go to another page

andika_kurniawan picture andika_kurniawan · Jun 16, 2015 · Viewed 14.4k times · Source

I want to user click table row then after it user will go to other page. The page has Storyboard ID "Other View" and Restoration ID "Other View". But I can't go to purposed view

This is my code :

 - (UITableViewCell *)tableView:(UITableView *)tabel cellForRowAtIndexPath:(NSIndexPath *)indeksBaris
{

    static NSString *simpleTableIdentifier = @"TampilanCell";
    TabelBeritaCell *cell = (TabelBeritaCell *)[tabel dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSLog(@"nilai : %d", indeksBaris.row );
    //detecting NIB of table view
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableView" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    //Go To View Controller which have identifier "Other View"
    UIViewController *otherViewCon;
    otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
    [self.navigationController pushViewController:otherViewCon animated:YES];

    cell.judulBerita.text =  [listBerita objectAtIndex:indeksBaris.row];

    return cell;
}

This code doesn't work :

UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];

UPDATED I have insert new code. I use didSelectRowAtIndexPath method but it doesn't work :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIViewController *otherViewCon;
        otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
        [self.navigationController pushViewController:otherViewCon animated:YES];
    }

Answer

Jessica picture Jessica · Jun 16, 2015

Try the method didSelectRowAtIndexPath

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

If you want to pass a variable or perform an action before it segues, do the following:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowDetail"]) {
        //Do something
        Detail *detailController = (Detail*)segue.destinationViewController;
    }
}

If you want to know how to name a segue, here is a great tutorial from apple docs: Naming Segues