我有一个头文件
@interface DemoFirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
在此头文件的源文件中,我声明了此方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AnotherViewController *anotherViewController=[[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
NSLog(@"didSelectRowAtIndexPath: row=%d", indexPath.row);
}
和另一个ViewController文件是
@interface AnotherViewController : UIViewController
{
IBOutlet UILabel *message;
}
@property (nonatomic , retain) UILabel *message;
@end
我这样做都是使用Xib文件。不是故事板。
这是tabbased Application
。并且已经在Appdelegate中声明了两个viewcontroller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
UIViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
在点击表格单元格时,另一个ViewController没有进入。 Plz尽快回复。
答案 0 :(得分:1)
这背后可能有几个原因:
UINavigationController
应该在Appdelegate
类中正确实现。UITableView
不应添加subView
的任何self.view
。如果正在调用didSelectRowAtIndexPath:
,那么其他方面也没关系你忘了设置tableView
个代表。
tableView.delegate = self;
编辑:我看了Zeel的评论,他说他正在使用TabBar
而他之前没有提到它所以我正在编辑我的答案: - < / p>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DemoFirstViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController1];
DemoSecondViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[nav1, nav2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
答案 1 :(得分:0)
NSLog(@"Navigation Controller: %@", self.navigationController);
检查此行打印的内容。 我怀疑你忘了在层次结构中添加navigationController
更新: 基于您的AppDelegate代码,更新它以解决您的问题:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
// here I create a Navigation Controller and set its root view controller to viewController1
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
// updated this line to show the navController1 (which contains viewController1)
self.tabBarController.viewControllers = @[navController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
如果甚至viewController2是一个需要推送东西的UITableViewController,在它上面做同样的事情(添加另一个UINavigationController,设置根视图控制器,并设置广告一个TabBarController的viewController)
答案 2 :(得分:0)
上述所有答案都指向了正确的方向。如果您还没有抓住它,请检查您是否在appdelegate
中添加了以下代码,
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
再次检查。