我正在编写一个iPad应用程序,它将有几个按钮,当点击时打开一个弹出框到桌面视图。用户将选择一个值,弹出窗口将被解除,按钮标题将更改。
我使用了一个Popover,然后又想添加另一个。我想写一些好的,干净的可重用代码。
我与代表们的关系很大。应该有多少?每个弹出窗口都应该有它自己的。
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
根视图控制器方法
#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方法
@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
非常感谢任何帮助或建议!!!
布赖恩
答案 0 :(得分:1)
对于可重复使用的代码,您只需要一个PopViewController
版本。你的两个版本看起来完全相同,所以摆脱一个版本。在InterfaceBuilder中,将按钮设置为所有segue到单个PopViewController。然后,Root View Controller看起来像这样:
@property (strong, nonatomic) IBOutlet UIButton *btnOpenPopover1;
@property (strong, nonatomic) IBOutlet UIButton *btnOpenPopover2;
在prepareForSegue:
中,而不是转到PopViewController
的一个版本或另一个版本,只需转到您的单个版本即可。向Root View Controller添加一个属性,以跟踪它(这取代了pvc1和pvc2):
@property (strong, nonatomic) PopViewController* currentPopoverController;
您还需要跟踪正在编辑的按钮,因此为Root View Controller添加一个属性(请注意弱引用):
@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];
}
答案 1 :(得分:0)
我想出来了。
我需要在h文件中输入我的segue,将其设置为segue标题,然后我将segue属性添加到prepareForSegue方法:
_segPopViewController = (UIStoryboardPopoverSegue *)segue;
最后在代表中我能够放入这一行:
[[_segPopViewController popoverController] dismissPopoverAnimated: YES];
成功!
对原始海报的称赞 - 你帮助了我一吨!我希望我能不止一次投票给你!!!
布赖恩