2011-02-14 110 views
5

我想從自定義按鈕中獲取背景圖像的名稱。如何從iPhone SDK上的UIButton獲取圖像名稱?

我把這種方法,當我點擊過的UIButton:

-(void)getButtonImageName:(id)sender{  
    UIButton *resultButton = (UIButton *)sender; 
    NSLog(@" The button's image is %@.", resultButton.currentImage); 
} 

我能拿到冠軍,並從按鈕的其他屬性,但不是背景圖像名稱。

任何人都知道該怎麼做?謝謝!

回答

1

沒有辦法將UIImage變成與[UIImage imageNamed:]一起使用的名稱。

你濫用圖片以存儲數據嗎?
你試圖用圖像的名稱來完成什麼?


編輯:

我有幾個按鈕,我想要得到的圖像名字,當我按下一個按鈕,切換到其他視圖控制器和加載圖像的名稱。

無需執行此操作,只需傳遞UIImage實例即可。
如果你想顯示基於名稱的不同圖像,就像你有一個縮略圖,並且想要在另一個視圖控制器中顯示大版本一樣,你必須從數據模型中獲取圖像名稱並將其傳遞給視圖控制器。
你如何決定哪個按鈕獲取哪個圖像?

+0

我有幾個按鈕,我想,當我得到的圖像名稱按一個按鈕,切換到另一個視圖控制器並加載該圖像的名稱。 – karse23 2011-02-14 09:26:34

0

@fluchtpunkt正如你所說,我有一些縮略圖是帶有背景圖像的UIbuttons。會發生什麼是我創建如下在前面的視圖控制器中選擇的類別。事情是這樣的......

if([strSection isEqualToString:@"animals"]){ 
     [button_thumbnail_1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:(UIControlState)normal]; 
     [button_thumbnail_2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:(UIControlState)normal]; 
    } 
    if([strSection isEqualToString:@"cars"]){ 
     [button_thumbnail_1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:(UIControlState)normal]; 
     [button_thumbnail_2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:(UIControlState)normal]; 
    } 
} 

感謝響應

0

你可以繼承的UIButton:

//.h 
@interface MySpecialButton : UIButton { 
    int mySpecialAttribute; 
} 

@property (nonatomic) int mySpecialAttribute; 

- (void) setBackgroundImage; 
- (UIImage*) getImage; 

@end 

,並使用您的特殊屬性都設置並返回映像名稱:

//.m 
@implementation LetterButton 

@synthesize mySpecialAttribute; 
- (void) setBackgroundImage { 

    [self setBackgroundImage:[self getImage] forState:UIControlStateNormal]; 
    [self setBackgroundImage:[self getImage] forState:UIControlStateDisabled]; 
} 

- (UIImage*) getImage { 
    NSString *fileName = [NSString stringWithFormat: 
      @"My_special_image_%i.png", mySpecialAttribute]; 

    return [UIImage imageNamed:fileName]; 
} 

@end 

在xib中設置按鈕的類別:

use of MySpecialButton

10

這是爲了得到一個自定義按鈕

-(void)yesButtonButtonCliked:(id)sender 
{ 
    UIButton *tmpYes = (UIButton *)sender; 
    [self ClearAllButtons:tmpYes.tag:@"YES"]; 
    UIImage* selectedImg=[UIImage imageNamed:@"checkboxtick.png"]; 
    if (tmpYes.currentImage == selectedImg) 
    { 

    } 
    else 
    { 
     [tmpYes setImage:[UIImage imageNamed:@"checkboxtick.png"] forState:UIControlStateNormal]; 
    } 
    NSLog(@"Button tag is %d",tmpYes.tag); 
} 
0

對馬蒂亞斯的回答繼當前圖像最簡單的做法「沒有必要做這個,只是通過UIImage的實例,」這是解決方案對我來說,與同胞newbs具體代碼:

Calling .m: 
    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
       //Create an instance of the destination controller 
       dmImageZoom *dmz = (dmImageZoom *)[segue destinationViewController]; 
       //Get the tapped button (the image was in a button) into a button object 
       UIButton *btnImg= sender; 
       //Get the button's image object 
       UIImage *img=btnImg.currentImage; 
       //Pass the image object to the destination controller's instance variable 
       dmz.imgPassed=img; 

Called .h: 
@interface dmImageZoom : UIViewController { 
    //Instance variable 
    UIImage *imgPassed; 
} 
    //Property for the object that presents the image 
    @property (strong, nonatomic) IBOutlet UIImageView *imgView; 
    //Property that enables it to be assigned a value 
    @property (strong, nonatomic) IBOutlet UIImage *imgPassed; 

Called .m: 

- (void)viewDidLoad 
{ 
    ... 
    imgView.contentMode = UIViewContentModeScaleAspectFit; 
    //Assign the image to view object 
    imgView.image=imgPassed; 
0
UIButton *senderButton = (UIButton*)sender;  
if ([senderButton.currentTitle isEqualToString:@"Hello"]) { 
      [senderButton setTitle:@"H" forState:UIControlStateNormal]; 
     } 
     else { 
      [senderButton setTitle:@"Hello" forState:UIControlStateNormal]; 
    forState:UIControlStateNormal]; 
     }