This post deals with using UIPopoverController in iPad programming..
UIPopoverController is an iPad specific controller. UIPopoverController is not actually a ViewController in itself but it manages the presentation of view controllers. Popovers provide a lightweight way to present or gather information from the user and are commonly used in the following situations:
- To display information about an object on the screen.
- To manage frequently accessed tools or configuration options
- To present a list of actions to perform on objects inside one of your views
- To present one pane from a split view controller when the device is in a portrait orientation
Now we will see how we can make a UIPopoverController as shown above
We will take two view controllers for this tutorial
The first view controller will be popoverViewController and second view controller will be popViewController
The left pane of Xcode will look something like this
Now first come to popViewController.h and write the following code as highlighted
“delegate” property will be assigned the object of popoverViewController class. So popoverViewController object will have the implementation of “-(void) dismiss:(popViewController *) controller;”. This method will be used to dismiss UIPopoverController. Also this is the view controller class which will be presented as a popover on the press of a button.
Open popViewController .xib, drag a table view on it as shown below and make the UITableViewDataSource and UITableViewDelegate connections
The above popViewController’s view will be shown as a popover and will change the value of the label in popoverViewController’s view on the selection of a row.
Now come to popViewController.m file
Populate the array with three values in viewDidLoad method
array=[[NSArray alloc] initWithObjects:@"JOHN",@"MIAMI",@"DEAN",nil];
Now write the following delegation methods of table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[array objectAtIndex:(indexPath.row)];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
popoverViewController *p=(popoverViewController *)self.delegate;
UILabel *label;
label=p.label;
label.text=[array objectAtIndex:indexPath.row];
[self.delegate dismiss:self];
NSLog(@"hello");
}
As we can see from the above code that the popover, which contains the content of popViewController class, will be containing three rows. Property delegate references popoverViewController object because it acts as the delegate for popViewController and implements “-(void) dismiss:(popViewController *) controller;” method as said above. This method will contain the code to dismiss the popover. So on selection of row, the value of the label in popoverViewController will be changed to the row’s value and popover will be dismissed.
Now come to popoverViewController.h file and write the following highlighted code
As this class acts as the delegate for popViewController class, so it implements popViewControllerDelegate. UIPopoverController’s object popover will be used to represent the content of popViewController class in popover style A label will be attached in popoverViewController.xib file which will change value on selection of row in popover.”pop:” method will be fired when bar button will be pressed and will show the popover.
Now come to popoverViewController.xib. Drag label and barbutton, and make connections as shown
Now open popoverViewController .m file
Implements the pop: method as shown below
-(IBAction)pop:(id)sender
{
popViewController *pop=[[popViewController alloc] initWithNibName:@"popViewController" bundle:nil];
pop.delegate=self;
UIPopoverController* aPopover = [[UIPopoverController alloc]initWithContentViewController:pop];
[pop release];
self.popover = aPopover;
self.popover.popoverContentSize=CGSizeMake(300, 500);
[aPopover release];
[self.popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
In the above code we made a popViewController object which will be presented in a popover style with the help of UIPopoverController object. We set the “self” object as the delegate for popViewController object. After this we set the popover size by using property “popoverContentSize”. At last we display the popover.
Implement the delegate method “-(void) dismiss:(popViewController *) controller;”
-(void) dismiss:(popViewController *) controller
{
[self.popover dismissPopoverAnimated:YES];
}
The above method will dismiss the popover
Save and build the application. Output is as shown below
Just click some row and you will see the value of label changing












