Best motive for several popovers in Storyboard

I am writing an iPad application which will have several buttons that when clicked open a popover to a tableview. The user will choose a value, and the popover will be dismissed and the button't title will change.

I got this working with one Popover, and then wanted to add another. I want to write some good, clean reusable code.

My big hangup is with delegates. How many should there be? Should each popover have its own.

Root View Controller Header

#import <UIKit/UIKit.h>
#import "PopViewController1.h"
#import "PopViewController2.h"

@interface RootViewController : UIViewController <PopViewControllerDelegate,UIPopoverControllerDelegate>

// Properties for accessing the popover and its viewcontroller (1)
@property (strong, nonatomic) IBOutlet UIButton *btnOpenPopover1;
@property (strong, nonatomic) UIStoryboardPopoverSegue *pvcSegue1;
@property (strong, nonatomic) PopViewController1 *pvc1;

// Properties for accessing the popover and its viewcontroller (2)
@property (strong, nonatomic) IBOutlet UIButton *btnOpenPopover2;
@property (strong, nonatomic) UIStoryboardPopoverSegue *pvcSegue2;
@property (strong, nonatomic) PopViewController2 *pvc2;

@end

Root View Controller Method

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setBtnOpenPopover1:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"segPop1"]) {
        _pvcSegue1 = (UIStoryboardPopoverSegue *)segue;
        _pvc1 = [segue destinationViewController];
        [_pvc1 setDelegate:self];

    } else ([[segue identifier] isEqualToString:@"segPop2"]); {
        _pvcSegue2 = (UIStoryboardPopoverSegue *)segue;
        _pvc2 = [segue destinationViewController];
        //[_pvc2 setDelegate:self];
        }
    }

// PopViewControllerDelegate callback function
- (void)dismissPop:(NSString *)value {

    [_btnOpenPopover1 setTitle:value forState:UIControlStateNormal];
    [[_pvcSegue1 popoverController] dismissPopoverAnimated: YES]; // dismiss the popover
}

@end

PopViewController1.h

#import <UIKit/UIKit.h>

@protocol PopViewControllerDelegate;

@interface PopViewController1 : UITableViewController

@property (weak) id <PopViewControllerDelegate> delegate;
@property (strong, nonatomic) NSString *strPassedValue;
@property (nonatomic, strong) NSMutableArray *importantChoices;

@end

@protocol PopViewControllerDelegate <NSObject>
@required
- (void)dismissPop:(NSString *)value;
@end

PopViewController1 Method

#import "PopViewController1.h"

@interface PopViewController1 ()

@end

@implementation PopViewController1

- (id)initWithCoder:(NSCoder *)aDecoder
{
    //Popover Choices
    _importantChoices = [NSMutableArray array];
    [_importantChoices addObject:@"Extremely Important"];
    [_importantChoices addObject:@"Very Important"];
    [_importantChoices addObject:@"Somewhat Important"];
    [_importantChoices addObject:@"Not Very Important"];
    [_importantChoices addObject:@"Not At All Important"];

    self.clearsSelectionOnViewWillAppear = NO;

    NSInteger rowsCount = [_importantChoices count];
    NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView
                                           heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSInteger totalRowsHeight = rowsCount * singleRowHeight;

    //Calculate how wide the view should be by finding how
    //wide each string is expected to be
    CGFloat largestLabelWidth = 0;
    for (NSString *colorName in _importantChoices) {
        //Checks size of text using the default font for UITableViewCell's textLabel.
        CGSize labelSize = [colorName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]];
        if (labelSize.width > largestLabelWidth) {
            largestLabelWidth = labelSize.width;
        }
    }

    //Add a little padding to the width
    CGFloat popoverWidth = largestLabelWidth + 100;

    //Set the property to tell the popover container how big this view will be.
    self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight);

    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Uncomment the following line to preserve selection between presentations.
        //self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear: (BOOL)animated
{

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_importantChoices 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];
    }
    cell.textLabel.text = [_importantChoices objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _strPassedValue = [_importantChoices objectAtIndex:indexPath.row];
    [_delegate dismissPop:_strPassedValue];
}

@end

PopViewController2 Header

#import <UIKit/UIKit.h>

@protocol PopViewControllerDelegate;

@interface PopViewController2 : UITableViewController

@property (weak) id <PopViewControllerDelegate> delegate;
@property (strong, nonatomic) NSString *strPassedValue2;
@property (nonatomic, strong) NSMutableArray *importantChoices2;

@end

@protocol PopViewControllerDelegate <NSObject>
@required
- (void)dismissPop2:(NSString *)value;
@end

PopViewController2 Method

import "PopViewController2.h"

@interface PopViewController2 ()

@end

@implementation PopViewController2

- (id)initWithCoder:(NSCoder *)aDecoder
{
    //Popover Choices
    _importantChoices = [NSMutableArray array];
    [_importantChoices addObject:@"Extremely Important"];
    [_importantChoices addObject:@"Very Important"];
    [_importantChoices addObject:@"Somewhat Important"];
    [_importantChoices addObject:@"Not Very Important"];
    [_importantChoices addObject:@"Not At All Important"];

    self.clearsSelectionOnViewWillAppear = NO;

    NSInteger rowsCount = [_importantChoices count];
    NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView
                                           heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSInteger totalRowsHeight = rowsCount * singleRowHeight;

    //Calculate how wide the view should be by finding how
    //wide each string is expected to be
    CGFloat largestLabelWidth = 0;
    for (NSString *colorName in _importantChoices) {
        //Checks size of text using the default font for UITableViewCell's textLabel.
        CGSize labelSize = [colorName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]];
        if (labelSize.width > largestLabelWidth) {
            largestLabelWidth = labelSize.width;
        }
    }

    //Add a little padding to the width
    CGFloat popoverWidth = largestLabelWidth + 100;

    //Set the property to tell the popover container how big this view will be.
    self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight);

    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Uncomment the following line to preserve selection between presentations.
        //self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear: (BOOL)animated
{

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_importantChoices 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];
    }
    cell.textLabel.text = [_importantChoices objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _strPassedValue = [_importantChoices objectAtIndex:indexPath.row];
    [_delegate dismissPop:_strPassedValue];
}

@end

Any help or suggestions would be greatly appreciated!!!

Bryan

For reusable code, you only want one version of PopViewController . Your two versions look identical, so get rid of one. In InterfaceBuilder, set your buttons to all segue to a single PopViewController. Root View Controller, then, looks like this:

@property (strong, nonatomic) IBOutlet UIButton *btnOpenPopover1;
@property (strong, nonatomic) IBOutlet UIButton *btnOpenPopover2;

In prepareForSegue: , rather than segue to one version or another of PopViewController , just segue to your single version. Add a property to Root View Controller, to keep track of it (this replaces pvc1 and pvc2):

@property (strong, nonatomic) PopViewController* currentPopoverController;

You also need to keep track of which button is being edited, so add a property to your Root View Controller for that (note the weak reference):

@property (weak, nonatomic) IBOutlet UIButton* currentButton;

Set both of these in prepareForSegue: , so it looks like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    currentPopoverController = [segue destinationViewController];
    [currentPopoverController setDelegate:self];

    currentButton = (UIButton*) sender;
}

Now, you only need one delegate that looks like this:

- (void)dismissPop:(NSString *)value {

    [currentButton setTitle:value forState:UIControlStateNormal];
    [currentPopoverController dismissPopoverAnimated:YES];
}