Search code examples
iphoneiosipaduipopovercontroller

Error on opening popviewcontroller in iOS 7


It looks a simple thing to me but I am not able to understand what mistake I am making. I have to open a popover on click of row in my iPad application. I have done following code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    popViewController *vc = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil];
    vc.preferredContentSize = CGSizeMake(500,500);
    vc.view.frame = CGRectMake(0, 0, 500, 500);

    UIPopoverController *healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

The app crashes when last line is executed. I have searched lots of pages on the web, but I am not able to find its cause. I am not getting any specific error only the Thread 1 error in the main file.

I am using iOS 7.


Solution

  • Try to add your healthPopOverit a member of your class since UIPopoverControllers must be held in an instance variable.

    In your .m define it as a property:

    UIPopoverController *healthPopOver;
    

    and change:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        popViewController *vc = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil];
        vc.preferredContentSize = CGSizeMake(500,500);
        vc.view.frame = CGRectMake(0, 0, 500, 500);
    
        self.healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }