用另一个视图为每个UITableViewCellbuild立一个链接
我是iOS编程的新手,我想做一件简单的事情。 我看到了几个有关我的问题的主题,但我不明白为什么我的代码不工作…
- 我用.xib创build了一个名为details2ViewController的UIViewController子类
- 在我的主视图叫ViewController.xib ,我有一个tableView
- 在ViewController.m中 ,我在顶部添加了:
#import "details2ViewController.h"
-
在ViewController.m中 ,我修改了didSelectRowAtIndexPath方法,如下所示:
details *det = [[details alloc] init]; [self.navigationController pushViewController:det animated:YES];
没有任何警告,但是当我点击一个单元格时没有任何效果…我确切地说我没有使用mainStoryBoard。
注意: 这是我以前的post关于这个问题。
(对不起,如果我的英文很尴尬,我是法语…感谢您的帮助!)
首先检查你是否正确地将你的TableView委托连接到你的ViewController的文件所有者或不。 TableView的-didSelectRowAtIndexPath
方法是一个委托方法。
其次,我不明白为什么当你已经有导入details2ViewController.h
details
作为一个类名称。 所以,它看起来像你应该使用details2ViewController
而不是details
,你的代码应该是这样的:
details2ViewController *det = [[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil]; [self.navigationController pushViewController:det animated:YES];
确保您的ViewController
确实embedded在UINavigationController
。 如果没有,那么你的self.navigationController will
是nil
。
或者你可以通过简介到UINavigationController 。
如果您正在使用Storyboard,则可以使用此代码。
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; YourViewController *yourViewController = [storyboard instantiateViewControllerWithIdentifier:@"StoryBoardID"]; [self.navigationController pushViewController:yourViewController animated:YES]; }
不要忘记在最后一行设置animated:BOOL
为YES
。
您必须先在AppDelegate.h中定义导航控制器的属性:
@property (strong, nonatomic) UINavigationController *controller;
然后在AppDelegate.m文件中,在didFinishLaunchingWithOptions:方法中执行此操作:
details2ViewController *controller=[[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil]; self.controller = [[UINavigationController alloc] initWithRootViewController:controller]; } self.window.rootViewController=self.controller; [self.window makeKeyAndVisible]; return YES;
然后在didSelectRowAtIndexPath中的细节2ViewController.m中:
details2ViewController *det = [[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil]; [self.navigationController pushViewController:det animated:YES];
请让我知道,如果它的工作.. :)谢谢。