Xcode for iPhoneアプリで次のことを行うより良い方法はありますか?
4 つのボタンがあるメイン ナビゲーション UIViewController 画面があります。
ボタン 1 をクリックすると、4 つのボタンがあるサブ ナビゲーション UIViewController が表示されます。ボタン 2 をクリックすると、同じサブ ナビゲーション UIViewController が表示されますが、5 つのボタンがあります。ボタン 3 をクリックすると、同じサブ ナビゲーション UIViewController が表示されますが、4 つのボタンがあります。ボタン 4 をクリックすると、同じサブナビゲーション UIViewController が表示されますが、6 つのボタンがあります。
これを処理するために、メイン ナビゲーションの各ボタンにタグを割り当てました。ボタンがクリックされると、このタグ番号を取得して、サブ ナビゲーションの UIViewController に渡します。
次に、サブ ナビゲーション UIViewController で、この値に基づいて、必要に応じてサブ ナビゲーション ボタンを手動で描画/作成します。
以下は、サブナビゲーション UIViewController でこれを処理する方法です。メイン ナビゲーション UIViewController から渡された値を確認し、それに応じてボタンの数を描画します。また、各ボタンのカスタム背景画像も設定します。ボタンのクリックごとに、独自のセレクター メソッドがあります。サブ ナビゲーションの一部のボタンは UIViewController に移動し、一部のボタンは TableViewController に移動することに注意してください。また、各サブ ナビゲーション ボタンは、その宛先ビューに独自の「コンテンツ」を表示する必要があります。
これを処理するためのより良い、よりエレガントな方法はありますか? 私にはコードの重複が多いように思えます。以下の例は、簡潔にするために短縮されています。
// SubNavViewController.m
// SegueTest
#import "SubNavViewController.h"
#import "GettingHereViewController.h"
#import "TableViewController.h"
#import "GettingHereContent.h"
@interface SubNavViewController ()
@end
@implementation SubNavViewController
@synthesize tagNumber;
@synthesize buttonNumber;
@synthesize buttonPushedTrackNumber;
@synthesize hotelButton;
@synthesize bAndBButton;
@synthesize caravanButton;
@synthesize selfCateringButton;
@synthesize apartmentsButton;
.
.
.
etc (19 in total)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.tagNumber == 1)
{
self.navigationItem.title = @"Getting Here";
// By Air Button
byAirButton = [UIButton buttonWithType:UIButtonTypeCustom];
byAirButton.tag = 1;
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:@"gettingHereByAirButton.png"];
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:byAirButton];
[byAirButton addTarget:self action:@selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside];
// By Rail Button
byRailButton = [UIButton buttonWithType:UIButtonTypeCustom];
byRailButton.tag = 2;
byRailButton.frame = CGRectMake(25, 190, 280.f, 40.f);
UIImage *railButton = [UIImage imageNamed:@"gettingHereByRailButton.png"];
[byRailButton setBackgroundImage:railButton forState:UIControlStateNormal];
[self.view addSubview:byRailButton];
[byRailButton addTarget:self action:@selector(byRailButtonClicked) forControlEvents:UIControlEventTouchUpInside];
.
.
. etc (2 more button)
}
else if (self.tagNumber == 2)
{
self.navigationItem.title = @"Where to Stay";
// B&B Button
bAndBButton = [UIButton buttonWithType:UIButtonTypeCustom];
bAndBButton.tag = 1;
bAndBButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *bedAndBreakfast = [UIImage imageNamed:@"whereToStayBedAndBreakfastButton.png"];
[bAndBButton setBackgroundImage:bedAndBreakfast forState:UIControlStateNormal];
[self.view addSubview:bAndBButton];
[bAndBButton addTarget:self action:@selector(bAndBButtonClicked) forControlEvents:UIControlEventTouchUpInside];
.
.
. etc (do this for the rest of the buttons)
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void) byAirButtonClicked
{
GettingHereContent *gettingHere = [GettingHereContent new];
gettingHere = @"By Air";
NSLog(@"Content: %@", gettingHere);
[self performSegueWithIdentifier:@"gettingHereSegue" sender:self];
}
- (void) bAndBButtonClicked
{
GettingHereContent *gettingHere = [GettingHereContent new];
gettingHere = @"Bed and Breakfast";
NSLog(@"Content: %@", gettingHere);
[self performSegueWithIdentifier:@"tableViewSegue" sender:self];
}
.
.
. (do this for all buttons - 19 in total)
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
@end