简体   繁体   English

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

[英]How to link UITableView Cell to different ViewControllers in Storyboard

I have created a UITableView and set everything up so that it pulls the correct Data that I want on the table. 我创建了一个UITableView并设置了所有内容,以便它在表格中提取我想要的正确数据。 The only issue I have is that when I click on an item, I want it to open that viewController 我唯一的问题是当我点击一个项目时,我希望它打开那个viewController

I have set the Storyboard ID up correctly so everything should work but please see my code below incase there's something obvious I've missed: 我已正确设置故事板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];

    }





}

I can provide more info if needed 如果需要,我可以提供更多信息

You use instantiateViewControllerWithIdentifier to create view controller objects. 您使用instantiateViewControllerWithIdentifier来创建视图控制器对象。 But you are not presenting the created view controller object so add: 但是您没有呈现创建的视图控制器对象,因此添加:

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

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

[self performSegueWithIdentifier:indentifier sender:nil];

You need to either Push yournewTopViewController 您需要推送yournewTopViewController

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

Or Modally Present your newTopViewController 或者模态显示你的newTopViewController

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

in the end of your didSelectRowAtIndexPath Method. 在didSelectRowAtIndexPath方法的最后。

To answer your question I believe you should first import all your .h files First. 要回答你的问题,我相信你应该首先导入你所有的.h文件。 Then you can go 然后你可以去

if (indexPath.section == 0) {

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

}

And so on and so forth 等等等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM