This post will cover following topics
- UIPopoverViewController
- UIBarButtonItems
- UIToolbar, UINavigationBar
- IBActions & IBOutlet collections
- Illustration using step by step
- Illustration using Video.
I will describe to the points & will not write too much.
01. Open existing project or create new project.
02. Go to your view-controller Interface/xib/nib file.
03. Add UIToolbar or UINavigationBar.
04. Add necessary UIBarButtonItems.
05. Connect Actions.
06. Now, create a new viewController – assume popVCtr is the class/interface name.
07. Open popVCtr.xib file & build the User-interface as per the need.
08. Open popVCtr.xib & place following code in viewDidLoad or viewWillAppear method.
1 2 3 4 5 6 |
- (void)viewDidLoad { [super viewDidLoad]; self.modalInPopover = NO; self.contentSizeForViewInPopover = CGSizeMake(300, 300); } |
09. Switch back to your view-controller.h file & declare variables as follows.
1 2 3 4 5 6 7 8 9 |
#import <UIKit/UIKit.h> #import "popVCtr.h" @interface ViewController : UIViewController @property (strong, nonatomic) IBOutletCollection(UIBarButtonItem) NSArray *anyBarButtonTapped; - (IBAction)anyButtonTapped:(id)sender; @property (strong, nonatomic) UIPopoverController *popVCtr; @property (strong, nonatomic) popVCtr *nxtpopVCtr; @end |
10. Now, switch to view-controller.m file & place following line of code in your IBAction.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- (IBAction)anyButtonTapped:(id)sender { // type casting. UIBarButtonItem *item = (UIBarButtonItem*)sender; // create popover if needed if(!self.popVCtr) { // first create view controller self.nxtpopVCtr= [[popVCtr alloc] initWithNibName:@"popVCtr" bundle:nil]; // assing view controller to popover self.popVCtr=[[UIPopoverController alloc] initWithContentViewController:self.nxtpopVCtr]; } // display popover now. [self.popVCtr presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } |
11. Run the project & check out following sample-output for comparison.
12. Download the sample code from here.
13. Check out following video illustration for the same.
Leave a Reply