How to use UIPopoverController in iPad programming?

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

How to use UIAlertView?

This Tutorial deals with how to use UIAlertView in your application…

Let us suppose our viewController class is myViewController and we want an alert on the press of a button

Drag a button from library to view in myViewController.xib

Now come to myViewController.h

Add the following highlighted lines of code

Now in myViewController.m

Add the implementation of “showAlert”

-(IBAction)showAlert
{
	UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert user" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"button1",@"button2",nil];
	[alert show];
	[alert release];
}

Also add the following delegate method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"Pressed button %d",buttonIndex);
}

The above delegate method will get called everytime you press some button on UIAlertView and will tell the index number of the button pressed

In myViewController.xib make the connections as shown

Save and run application. Output is shown below

Now see how delegation method works. Suppose buttons are pressed in order   button1->Show Alert->button2->Show Alert->OK

then see the output on console

 

 

How to use UIPickerView?

This post deals with using UIPickerView in an application

 

Lets say the ViewController class in which UIPickerView is to be populated is myViewController

Open myViewController.xib

Drag UIPickerView from Library and add it to the view as shown below

iPhone Application Tutorials

Now come to myViewController.h file

Add the following lines of code highlighted

Now move to myViewController.m

Add the following code

@synthesize picker;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return @"john";
}

The above delegate methods take care of following conditions

In the above code we have assumed that number of components is 1 and there are 3 rows in that component

We want to populate the rows with “john”

Save all this

Now it’s time to make connections, so come again to myViewController.xib

Make connections as shown below

iphone Application Tutorials

Save and execute and the output is shown below

iPhone Application Tutorials