Search code examples
iosobjective-cuitableviewdetailview

Application tried to push a nil view controller on target


I am creating an app without storyboards and I am having trouble moving from when a user clicks on a table view cell to the detail view of that cell.

in the class where I create and handle the table view I have the following code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonDetailViewController *detailViewController;
    Person *person = [self.playerArray objectAtIndex:indexPath.row];
    detailViewController.person = person;
    [[self navigationController] pushViewController:detailViewController animated:YES];

}

So I am trying to push a detail view controller on to the screen and then display the user info on that detail screen. I have tried to find a solution to this but, most of the answers I have come across use storyboards, and I am not using storyboards.

in my PersonDetailViewController.m class I have the following code:

#import "PersonDetailViewController.h"

@interface PersonDetailViewController ()

@end

@implementation PersonDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.personInfo = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
    _personInfo.backgroundColor = [UIColor whiteColor];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

I am not sure if this may be a cause for the issue but in my delegate I have the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.tableViewController = [[TableViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.tableViewController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

I was thinking that maybe it has something to do with creating the navigation controller but I was playing around with it and I do not think it does.


Solution

  • You missed the initialization of the view controller in the tableView:didSelectRowAtIndexPath: method

    PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] init]; // or initWith...