1

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
4

1 に答える 1

1

最初に、次の方法ですべてのボタンを定義する NS_ENUM を作成することをお勧めします。

typedef NS_ENUM(NSUInteger, ButtonTag)
{
    ButtonTagHotel,
    ButtonTagOther,
    ...
}

次に、viewDidLoad: で、次のように現在の状態に必要なすべてのボタンを含む配列を作成できます。

NSMutableArray *buttonTags = [[NSMutableArray alloc] init];

if (self.tagNumber == 1 || self.tagNumber == 3)
    [buttonTags addObject: @(ButtonTagHotel)];

if (self.tagNumber == 5 || self.tagNumber == 8)
    [buttonTags addObject: @(ButtonTagOther)];

// etc...

必要なボタン タグを作成したら、それらすべてを 1 つのループで作成および追加できます。

uint cnt = 0;

for (NSNumber *tag in buttonTags)
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = [tag unsignedIntValue];
    btn.frame = CGRectMake(25, 20 + (cnt * 50.f), 280.f, 40.f);
    UIImage *image = [UIImage imageNamed:@"genericButtonImage.png"];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

    cnt++;
}

ボタンごとに異なる画像を設定する必要がある場合は、ButtonTag 列挙型によってインデックス付けされたすべての画像名を保持する別の配列を作成する必要があります...

-(void)buttonTouched:(UIButton*)sender: を実装するだけです。

-(void)buttonTouched:(UIButton*)sender
{
    switch (sender.tag) {
        case ButtonTagHotel:
        {
            // do your stuff
        }
            break;
        ...
        default:
            break;
    }
}
于 2013-11-02T10:35:31.190 に答える