objective c - click table row and go to another page

陌路散爱 提交于 2019-12-06 03:15:39

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

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];
}
Pradumna Patil

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

 -(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];
}

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];
   }
}
Sagar Sangani

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];
}
user3752049

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!