Search This Blog

Nov 19, 2010

How to Insert UITableView in popoverController and show popoverController on a particular UIButton.

This code shows that how to use popoverController. In this example we show popoverController on a UIButton click. A UITableView will be inserted in this popoverController, we will also show UIButton, UILable or any thing else in the popoverController.

-(IBAction)btnTableMenu_TouchUpInside:(id)sender{

    if([self.popoverController isPopoverVisible])
    {
        [self.popoverController dismissPopoverAnimated:YES];
        return;
    }   
    //build our custom popover view
    UIViewController* popoverContent = [[UIViewController alloc]init];
    UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 102)];
    popoverView.backgroundColor = [UIColor blackColor];
   
    UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 140, 102)];
    tblViewMenu.delegate = self;
    tblViewMenu.dataSource = self;
    tblViewMenu.rowHeight = 32;
    [popoverView addSubview:tblViewMenu];
    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(140, 102);
    self.popoverController = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];   
    //present the popover view non-modal with a
    //refrence to the toolbar button which was pressed
    [self.popoverController  presentPopoverFromRect:CGRectMake(0, 0, 133, 29)
                                             inView:btnTableMenu permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   
    //release the popover content
    [popoverView release];
    [popoverContent release];
}

1 comment:

  1. Sorry, How have I to do if I want to load a DetailView into Popover when I click the button in the tableView ?

    ReplyDelete