目标c – 点击表格行并转到另一页

我想用户点击表格行然后用户将转到其他页面。 该页面具有故事板ID“其他视图”和恢复ID“其他视图”。 但我不能去的目的

这是我的代码:

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

此代码不起作用:

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

更新我已经插入新的代码。 我使用didSelectRowAtIndexPath方法,但它不起作用:

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

试试didSelectRowAtIndexPath方法

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

如果您想在传递variables之前传递variables或执行操作,请执行以下操作:

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

如果你想知道如何命名Segue ,下面是一个来自苹果文档的优秀教程: 命名Segues

你的代码似乎是正确的。 只是我觉得在寻找UIStoryboard对象的问题.replace下面的东西可能会工作正常。

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

你应该写导航代码

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

这个方法。

或者你可以通过在storyboard中绘制push segue来直接转到另一个视图控制器。无需编写代码。

如何在selectUITableViewCell时进行推送

didSelectRowAtIndexPath方法中实现你的代码。 在你提出问题之前,请在编写代码之前仔细阅读文档。

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

尝试这个帮助你。 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]; } } 

在这里showDataVC是我的第二个ViewController的新文件名。 首先创build一个新文件的对象并初始化。 并使用pushviewcontroller中的对象来初始化showDataVC。

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