objective c - 单击表格行并转到另一页面

and*_*wan 3 objective-c tablerow ios

我想用户点击表格行然后用户将转到其他页面.该页面具有故事板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;
}
Run Code Online (Sandbox Code Playgroud)

此代码不起作用:

UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
Run Code Online (Sandbox Code Playgroud)

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

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

Jes*_*ica 5

试试这个方法 didSelectRowAtIndexPath

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

如果要在变量发送之前传递变量或执行操作,请执行以下操作:

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

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

  • 下选民可以解释一下吗? (2认同)