objective c - 单击 table 行并转到另一页

objective c - click table row and go to another page

我想让用户点击 table 行 然后用户将转到其他页面。该页面具有 Storyboard ID "Other View" 和 Restoration ID "Other View"。 但是我不能去目的视图

这是我的代码:

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

如果你想在 segues 之前传递变量或执行操作,请执行以下操作:

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

如果你想知道如何命名一个 segue,这里有一个来自苹果文档的很棒的教程:Naming Segues

didSelectRowAtIndexPath 方法中实现您的代码。并且请在编写代码之前阅读文档 n 研究然后再在这里提出问题。

你应该在

中编写导航代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

这个方法。

或者在storyboard.No需要写代码的地方直接通过drawing push segue转到另一个view controller

How to make a push segue when a UITableViewCell is selected

您的代码似乎是正确的。只是我觉得找不到 UIStoryboard 对象有问题。替换以下内容可能会正常工作。

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

{
      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 的新文件名。 在第一个创建新文件的对象并初始化。 并使用对象初始化 showDataVC 以在 pushviewcontroller 中使用对象。

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

    [self.navigationController pushViewController:objshowDataVc animated:YES];
}