3

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];
    }
andika_kurniawan
  • 511
  • 4
  • 9
  • 20

7 Answers7

5

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

Jessica
  • 9,379
  • 14
  • 65
  • 136
  • This code works. But I have error "'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'Other View'" Actually, I have create Storyboard ID and Restoration ID in the view named with 'Other View' – andika_kurniawan Jun 16 '15 at 07:19
  • Select the segue in the storyboard, then at the upper right hand side button and name the segue there. Here is a good tutorial on naming segues: https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/chapters/StoryboardSegue.html – Jessica Jun 16 '15 at 07:21
  • Don't be sorry!! We've all been there!! Just let me know if this gives the right answer – Jessica Jun 16 '15 at 07:28
  • 1
    I think the down voter on this post is just jealous that you are a woman and you can code :) Anyway, I think this post is good so I will upvote. –  Jun 16 '15 at 07:57
  • 1
    Thanks!! Appreciative it!! – Jessica Jun 16 '15 at 07:59
1

Your code seems to be correct. just i feel problem in finding UIStoryboard object .replace following things may this work fine.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIViewController *otherViewCon;
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"your_storyboard_name" bundle:nil];// like Main.storyboard for used "Main"
        otherViewCon = [storyboard instantiateViewControllerWithIdentifier:@"Other View"];
        [self.navigationController pushViewController:otherViewCon animated:YES];
}
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
0

You should write navigation code in

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

this method.

Or you can directly go to another view controller by drawing push segue in storyboard.No need to write code.

How to make a push segue when a UITableViewCell is selected

Community
  • 1
  • 1
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
0
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
      NSString * storyboardIdentifier = @"storyboardName";// for example "Main.storyboard" then you have to take only "Main"
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: [NSBundle mainBundle]];
      UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"secondVCStoryboardID"];
     [self presentViewController:UIVC animated:YES completion:nil];
}
Vaibhav Shiledar
  • 939
  • 8
  • 15
0

Try this its help you. didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
  if(indexpath.row==0)//means you click row 0 
   {
     UIViewController *otherView=[self.storyboard instantiateViewControllerWithIdentifier:@"otherview"];
        [self presentViewController:otherView animated:YES completion:nil];
   }
  else
   {
     UIViewController *detailView=[self.storyboard instantiateViewControllerWithIdentifier:@"detailView"];
        [self presentViewController:detailView animated:YES completion:nil];
   }
}
vky
  • 475
  • 1
  • 6
  • 14
0

in here showDataVC is a my new file name for second ViewController. in a first created a object of new file and initialize. and initialize showDataVC with object to use object in pushviewcontroller.

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    showDataVc *objshowDataVc = [self.storyboard instantiateViewControllerWithIdentifier:@"showDataVc"];

    [self.navigationController pushViewController:objshowDataVc animated:YES];
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Sagar Sangani
  • 287
  • 2
  • 7
-1

Implement your code in didSelectRowAtIndexPath method. And please read the documentations before writing code n research before you put questions here.

Vinodh
  • 5,262
  • 4
  • 38
  • 68
user3752049
  • 167
  • 1
  • 14