XCode- Send data and Close Popover from Master View Controller

Hello,

Recently I was working on the app, where there was a button in toolbar which opens the Pop over with a table view. After selecting table cell, we need to pass some information to master view controller and close the Popover.

First you need a reference in your master view controller. Add following to your View controller header file.

@property (nonatomic, strong) UIPopoverController *myPopOver;
Synthesize it in .m file.
@implementation masterViewController

@synthesize myPopOver; 
@end
Now add prepareForSegue method.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ 
   
    if([segue.identifier isEqualToString:@”popOverSegue”]){
        UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
        self.myPopOver = popoverSegue.popoverController;
        [segue.destinationViewController setDelegate:self];
    }
    
}

Also add one call call back method in your master view controller, which will be invoked when user selects a cell in pop over table view.
-(void)myCallBack{
}
Now in pop over table view controller import your master view controller and set delegate as follow.
#import
#import “masterViewController.h”
@interface myPopOverController : UITableViewController 
@property (nonatomic, weak) masterViewController* delegate;
@end

Add following code to .m file
#import “myPopOverController.h”
#import “masterViewController.h”
@interface  myPopOverController ()<UITableViewDelegate>
@end
@implementation iPadCategoriesPopOver
@synthesize categoriesArray,categoriesTableView,delegate;
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        [[self delegate] myCallBack];
    
}

@end
Check above code carefully, here we have delegate of type mater view controller and using it to invoke our call back function, Above code is executed when some one selects a row from table view. Now add following code to myCallBack function to close the pop over.
[self. myPopOver dismissPopoverAnimated:YES];

Also note that you can pass any parameter in callback if you want.
Hope this helps you.

This entry was posted in Apple, iOS, Objective C, Xcode on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

Leave a Reply

Your email address will not be published. Required fields are marked *