Zeel
Zeel

Reputation: 234

Push a new view controller on tableview cell tap

I have one header file which is

@interface DemoFirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

In the source file of this header file i have declare this method

- (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);

}

and anotherViewController file is

@interface AnotherViewController : UIViewController
{
    IBOutlet UILabel *message;
}

@property (nonatomic , retain) UILabel *message;

@end

I am doing this all using Xib file. not storyboard.

This is tabbased Application. and two viewcontroller are already declared in 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 on tapping of table cell, anotherViewcontroller is not comming. Plz reply as soon as possible.

Upvotes: 1

Views: 2920

Answers (3)

TheTiger
TheTiger

Reputation: 13354

There may be few reasons behind this:

  1. UINavigationController should be correctly implemented in Appdelegate class.
  2. UITableView should not be add on any subView of self.view.
  3. If didSelectRowAtIndexPath: is being called it is okay other wise You have forgotten to set tableView delegates.

    tableView.delegate = self;
    

EDIT: I read Zeel's comment he said he is using TabBar and he didn't mention it before So I'm editing my answer:-

- (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;
}

Upvotes: 1

LombaX
LombaX

Reputation: 17364

NSLog(@"Navigation Controller: %@", self.navigationController);

Check what is printed by this line. I suspect you have forgotten to add the navigationController in your hierarchy

UPDATE: Basing on your AppDelegate code, update it like this to solve your problem:

- (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;

}

If even viewController2 is an UITableViewController that needs to push something, do the same thing on it (add another UINavigationController, set the root viewcontroller, and set ad a viewController of the TabBarController)

Upvotes: 0

Breakpoint
Breakpoint

Reputation: 1541

All the answers above are pointing in the right direction. Just in case you still haven't gotten hold of it, check if you have added the following piece of code in your appdelegate,

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

and check again.

Upvotes: 0

Related Questions