1

我有一个头文件是

@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 在点击表格单元格时,anotherViewcontroller 没有出现。请尽快回复。

4

3 回答 3

1

这背后可能有几个原因:

  1. UINavigationController应在Appdelegate课堂上正确实施。
  2. UITableView不应添加任何subView.self.view
  3. 如果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;
}
于 2013-01-05T10:13:10.380 回答
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 的视图控制器)

于 2013-01-05T10:12:38.693 回答
0

以上所有答案都指向正确的方向。以防万一您还没有掌握它,请检查您是否在appdelegate,

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];  
self.window.rootViewController = navigationController;

并再次检查。

于 2013-01-05T10:30:45.437 回答