如何将UITableView Cell链接到Storyboard中的不同ViewControllers

时间:2013-12-30 13:51:10

标签: ios iphone objective-c xcode ipad

我创建了一个UITableView并设置了所有内容,以便在表格中提取我想要的正确数据。我唯一的问题是,当我点击一个项目时,我希望它打开viewController

我已正确设置故事板ID,所以一切都应该有效,但请看下面的代码,因为有一些明显我错过了:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];

     */

    UIViewController *newTopViewController;


    if (indexPath.section == 0) {

        NSString *identifier = [NSString stringWithFormat:@"%@", [self.section1 objectAtIndex:indexPath.row]];

        newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    } else if (indexPath.section == 1) {

        NSString *identifier = [NSString stringWithFormat:@"%@", [self.section2 objectAtIndex:indexPath.row]];

        newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    }

    else if (indexPath.section == 2) {

        NSString *identifier = [NSString stringWithFormat:@"%@", [self.section3 objectAtIndex:indexPath.row]];

        newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    }





}

如果需要,我可以提供更多信息

4 个答案:

答案 0 :(得分:0)

使用instantiateViewControllerWithIdentifier创建视图控制器对象。 但是您没有呈现创建的视图控制器对象,因此请添加:

[self.navigationController presentViewController:newTopViewController animated:YES completion:nil];

答案 1 :(得分:0)

如果标识符正确并且您在故事板中设置了segues,那么您只需调用

即可
[self performSegueWithIdentifier:indentifier sender:nil];

答案 2 :(得分:0)

您需要推送yournewTopViewController

    [self.navigationController pushViewController:newTopViewController animated:YES];

或以模态方式呈现您的newTopViewController

    [self.navigationController presentViewController:newTopViewController animated:YES completion:nil];

在didSelectRowAtIndexPath方法的末尾。

答案 3 :(得分:0)

要回答您的问题,我相信您应该首先导入所有.h文件。 然后你可以去

if (indexPath.section == 0) {

   FirstViewController *firstController = [[FirstViewController alloc] init];
   [self presentViewController:firstController animated:YES completion:nil];

}

依旧等等