2013-02-09 133 views
0

我有3個按鈕代表3個不同的圖像。只要輕按按鈕,圖像就會顯示出來。我的問題是,如何使用if()和NSArray/NSMutableDictionary/UIButton標籤或其他方法來縮短代碼。點擊按鈕時顯示圖像?

- (id)initWithFrame:(CGRect)frame 
{ 
    _button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _button1.frame = CGRectMake(20, 250, 50, 50); 
    [_button1 addTarget:self action:@selector(button1Tapped) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:_button1]; 

    _button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _button2.frame = CGRectMake(140, 250, 50, 50); 
    [_button2 addTarget:self action:@selector(button2Tapped) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:_button2]; 

    _button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _button3.frame = CGRectMake(210, 250, 50, 50); 
    [_button3 addTarget:self action:@selector(button3Tapped) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:_button3]; 
} 

- (void)button1Tapped 
{ 
    UIImage *_image = [UIImage imageNamed:@"IMAGE_1"]; 
    _imageView = [[UIImageView alloc] initWithImage:_image]; 
    _imageView.frame = CGRectMake(0, 0, 256, 384);   
    [self addSubview:_imageView]; 
} 

- (void)button2Tapped 
{ 
    UIImage *_image = [UIImage imageNamed:@"IMAGE_2"]; 
    _imageView = [[UIImageView alloc] initWithImage:_image]; 
    _imageView.frame = CGRectMake(0, 0, 256, 384);   
    [self addSubview:_imageView]; 
} 

- (void)button3Tapped 
{ 
    UIImage *_image = [UIImage imageNamed:@"IMAGE_3"]; 
    _imageView = [[UIImageView alloc] initWithImage:_image]; 
    _imageView.frame = CGRectMake(0, 0, 256, 384);   
    [self addSubview:_imageView]; 
} 

謝謝。

+0

呵呵,今天是重構日。剛剛回答了10分鐘前的類似問題。 – 2013-02-09 22:30:04

+1

BTW:使用數組來存儲按鈕,然後**使用**操作方法的'sender'參數。 – 2013-02-09 22:30:31

+0

@ H2CO3謝謝你的洞察力。您的重構答案對我的代碼的其他部分非常有用! – askingtoomuch 2013-02-10 02:08:57

回答

1

設置像這樣的圖像數組。讓它成爲你班級的一個財產。另外,也儘早建立你的圖像視圖。

@property(nonatomic, strong) NSArray *images; 
@property(nonatomic, strong) UIImageView *imageView; 

- (id)initWithFrame:(CGRect)frame { 

    self.images = [NSArray arrayWithObjects:[UIImage imageNamed:@"IMAGE_1"], [UIImage imageNamed:@"IMAGE_2"], [UIImage imageNamed:@"IMAGE_3"], nil]; 

    _imageView = [[UIImageView alloc] initWithImage:[self.images objectAtIndex:0]]; 
    _imageView.frame = CGRectMake(0, 0, 256, 384);   
    [self addSubview:_imageView]; 
} 

在創建按鈕,給他們這樣的標籤...

_button1.tag = 1; 
_button2.tag = 2; 
_button3.tag = 3; 

而且在創建按鈕的時候,讓他們都使用自來水一樣選擇...

[_button1 addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside]; 
[_button2 addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside]; 
// etc 

在水龍頭上,標籤1將成爲數組索引...

- (void)buttonTapped:(id)sender { 

    NSUInteger tag = ((UIButton *)sender).tag; 
    UIImage *image = [self.images objectAtIndex:tag]; 
    self.imageView.image = image; 
} 
+0

Got it!非常感謝。從未想過會在幾個小時內得到答案! – askingtoomuch 2013-02-10 02:05:41

+0

我必須用'[sender tag]'替換'sender.tag'才能使它工作。他們不一樣嗎? – askingtoomuch 2013-02-10 14:31:09

+0

是的。這是一樣的。這是因爲該類型是通用的id類型。我編輯顯示更正確的語法。 – danh 2013-02-10 15:24:46