2013-11-02 6 views
1

iPhone 응용 프로그램 용 Xcode에서 다음을 수행하는 더 좋은 방법이 있습니까?Xcode에서 버튼 만들기

4 개의 버튼이있는 메인 탐색 UIViewController 화면이 있습니다.

버튼 1을 클릭하면 네 개의 버튼이있는 하위 탐색 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 
+0

얼마나 간단하게 할 수 있는지는 네 개의 하위 탐색보기 컨트롤러에 중복이 있는지 여부에 따라 다릅니다. 그들 중 일부는 다른 버튼과 같은 버튼을 가지고 있습니까? 버튼 중 일부가 동일한 컨트롤러에 연결되어 있습니까? 아니면 모두 다릅니 까? 단순화 할 수있는 한 가지는 @synthesize 문을 모두 삭제하는 것이며, 더 이상 필요하지 않습니다. – rdelmar

+0

모든 버튼은 다를 것이지만 대부분은 동일한 컨트롤러로 연결됩니다. 4 개의 버튼 만 다른 컨트롤러와 연결됩니다. 또한 모든 @synthesize 문을 삭제하려고 시도했지만 오류가 발생하여 지금은 남겨 둘 것입니다. – heyred

+0

예를 들어 hotelButton과 같은 속성을 참조하기 때문에 이러한 오류가 발생했을 수 있습니다. 즉, self를 사용해야합니다.속성에 액세스하는 경우 hotelButton을, 뒷받침하는 ivar에 직접 액세스하려면 _hotelButton을 선택합니다. – rdelmar

답변

1

우선은 다음과 같은 방법으로 모든 버튼을 정의하는 NS_ENUM를 만드는 것이 좋습니다 :있는 viewDidLoad에서 다음

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

: 당신은 당신의 현재에 필요한 모든 버튼과 배열을 만들 수 있습니다 당신이 당신의 필요 버튼 태그를 구축있을 때

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... 

가 생성 한 루프에서 그들 모두를 추가 할 수 있습니다 : 다음과 같은 방법으로 같은 상태

당신은 모든 버튼에 대해 서로 다른 이미지를 설정해야하는 경우에, 당신은 ButtonTag 열거 색인 모든 이미지의 이름을 들고 다른 배열을 만들어야합니다
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++; 
} 

...

지금 당신은 그냥 구현해야 - (void) buttonTouched : (UIButton *) 송신자 :

-(void)buttonTouched:(UIButton*)sender 
{ 
    switch (sender.tag) { 
     case ButtonTagHotel: 
     { 
      // do your stuff 
     } 
      break; 
     ... 
     default: 
      break; 
    } 
}