0

我正在编写一个 iPad 应用程序,它将有几个按钮,当单击这些按钮时,会打开一个到 tableview 的弹出窗口。用户将选择一个值,弹出框将被关闭并且按钮的标题将改变。

我用一个 Popover 来解决这个问题,然后想添加另一个。我想写一些好的、干净的可重用代码。

我最大的挂断是与代表。应该有多少?每个popover是否应该有自己的。

根视图控制器头

#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

根视图控制器方法

#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 方法

#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 标头

#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 方法

导入“PopViewController2.h”

@interface PopViewController2 ()

@结尾

@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

任何帮助或建议将不胜感激!!!

布莱恩

4

2 回答 2

1

对于可重用代码,您只需要一个版本的PopViewController. 你的两个版本看起来一样,所以去掉一个。在 InterfaceBuilder 中,将您的按钮设置为全部连接到单个 PopViewController。根视图控制器看起来像这样:

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

prepareForSegue:中,而不是继续到 的一个或另一个版本PopViewController,只需继续到您的单个版本。向根视图控制器添加一个属性,以跟踪它(这将替换 pvc1 和 pvc2):

@property (strong, nonatomic) PopViewController* currentPopoverController;

您还需要跟踪正在编辑的按钮,因此为此向您的根视图控制器添加一个属性(注意弱引用):

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

将这两个都设置在 中prepareForSegue:,所以它看起来像这样:

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

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

    currentButton = (UIButton*) sender;
}

现在,您只需要一个如下所示的委托:

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

    [currentButton setTitle:value forState:UIControlStateNormal];
    [currentPopoverController dismissPopoverAnimated:YES];
}
于 2013-10-25T12:13:30.393 回答
0

我想通了。

我需要将我的 segue 放入 h 文件中,将其设置为 segue 标题,然后将 segue 属性添加到 prepareForSegue 方法:

_segPopViewController = (UIStoryboardPopoverSegue *)segue;

最后,在代表中,我能够放入这一行:

[[_segPopViewController popoverController] dismissPopoverAnimated: YES];

和成功!

感谢原始海报 - 你帮了我很多!我希望我能给你投票不止一个!!!

布莱恩

于 2013-10-29T19:45:06.577 回答