Naresh Kumar Koppera
Naresh Kumar Koppera

Reputation: 417

Application tried to push a nil view controller on target in didSelectRowAtIndexPath

We have two UIViewControllers

.When we click on the UItableViewCell we want it to segue to the SecondViewController.

We tried like this

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard;

   destinationController = [[UserDetails alloc] init];
   destinationController = (UserDetails *)[storyboard instantiateViewControllerWithIdentifier:@"User"];
   destinationController.FriendlyName.text=[usernameFriendlyName objectAtIndex:indexPath.row];
   UIBarButtonItem * backButton =[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
   destinationController.navigationItem.backBarButtonItem = backButton;
  [self.navigationController pushViewController:destinationController animated:TRUE];
}

Please guide to us .What wrong in our code.

Upvotes: 0

Views: 236

Answers (1)

iAnurag
iAnurag

Reputation: 9346

No need to do all this. Just connect the two viewControllers with the Push segue. Give some identifier to segue and simply write this line in didSelectRowAtIndexPath

[self performSegueWithIdentifier: @"Your segue identifier" sender: self];

To pass data from one view to other use PrepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"Your segue identifier"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

Upvotes: 2

Related Questions