我有一个头文件,它是
@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 中声明
- (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;
}
bt在点击表格单元格时,另一个View控制器未合并。请尽快回复。
这背后可能有几个原因:
UINavigationController
应该在Appdelegate
类中正确实现UITableView
不应添加到self.view
的任何subView
上-
如果正在调用
didSelectRowAtIndexPath:
,则其他方面也可以。您忘记设置tableView
代理tableView.delegate = self;
编辑:我读了Zeel的评论,他说他正在使用TabBar
,他以前没有提到过,所以我正在编辑我的答案:-
- (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;
}
以上所有答案都指向正确的方向。万一你还没有掌握它,请检查你是否在appdelegate
、中添加了以下代码
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
然后再次检查。
NSLog(@"Navigation Controller: %@", self.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,设置根viewcontroller,并设置TabBarController的viewcontroller(