tranvutuan
tranvutuan

Reputation: 6109

getting view controller nil when doing presentViewController

Each row clicked will lead you to another view controller

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if( indexPath.row == 0 ){
        titleController     = [[TitleFundraiserController alloc] initWithNibName:@"TitleFundraiserController" bundle:nil];
        [self.navigationController presentViewController:titleController animated:YES completion:nil];
    }
    if( indexPath.row == 1 ) {
        recipientController = [[RecipientController alloc] initWithNibName:@"RecipientController" bundle:nil];
        [self.navigationController presentViewController:recipientController animated:YES completion:nil];
    }
    if( indexPath.row == 2 ) {
        fundController      = [[FundingController alloc] initWithNibName:@"FundingController" bundle:nil];
        [self.navigationController presentViewController:recipientController animated:YES completion:nil];
    }
    if( indexPath.row == 3 ) {
        locationController  = [[LocationController alloc] initWithNibName:@"LocationController" bundle:nil];
        [self.navigationController presentViewController:locationController animated:YES completion:nil];
    }  
}

However, sometimes I am getting this error from the console and my program is crashing

  Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
 'Application tried to present a nil modal view controller on target <UINavigationController: 0x7c7a340>.'

I dont know why it is saying so .

Please help if you have experienced this issue before.

Thanks

Upvotes: 1

Views: 910

Answers (2)

shabbirv
shabbirv

Reputation: 9098

You are presenting recipientController in your if (indexPath.row == 2) statement when you should be presentingfundController

Here is the corrected code:

if( indexPath.row == 2 ) {
        fundController = [[FundingController alloc] initWithNibName:@"FundingController" bundle:nil];
        [self.navigationController presentViewController:fundController animated:YES completion:nil];
}

You are getting that error, because recipientController would be nil when presenting here

Upvotes: 1

Oscar Gomez
Oscar Gomez

Reputation: 18488

The only thing I see wrong with your code is that after presentViewController, you need to release the variable as well:

if ( indexPath.row == 0 ) {
            titleController     =   [[TitleFundraiserController alloc] initWithNibName:@"TitleFundraiserController" bundle:nil];
            [self.navigationController presentViewController:titleController animated:YES completion:nil];
            [titleController release];
    }

I don't see any reason for the error you are getting being random, isn't it always in the same row?. What is happening is the ViewController is not being loaded from the nib and therefore returning nil.

Upvotes: 1

Related Questions