クリックするとテーブルビューにポップオーバーを開くいくつかのボタンを持つiPadアプリケーションを作成しています。ユーザーが値を選択すると、ポップオーバーが閉じられ、ボタンのタイトルが変更されます。
これを 1 つのポップオーバーで動作させた後、別のポップオーバーを追加したいと考えました。きれいで再利用可能な優れたコードを書きたいと思っています。
私の大きな問題は、デリゲートにあります。いくつあるべきですか?各ポップオーバーには独自のものがあります。
ルート ビュー コントローラー ヘッダー
#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 メソッド
import "PopViewController2.h"
@インターフェイス 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
どんな助けや提案も大歓迎です!!!
ブライアン