2012-09-22 90 views
4

目前我正在iPhone應用程序中創建兩個按鈕併爲兩個按鈕設置圖像,當用戶觸摸button2然後在button2內顯示button1圖像時,我最好地嘗試了我的級別,但我無法修復此問題,請任何一個幫我如何在iPhone中獲取按鈕背景圖片?

由於提前

我嘗試這樣做:

 btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn1 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal]; 
    btn1.frame=CGRectMake(61, 60, 50, 50); 
    [btn1 addTarget:self action:@selector(Button1Method) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:btn1]; 

    btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn2 setImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal]; 
    btn2.frame=CGRectMake(112, 60, 50, 50); 
    [btn2 addTarget:self action:@selector(Button2Method) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:btn2]; 



-(void)Button2Method 
{ 

[btn2 setImage:[UIImage imageNamed:btn1.currentBackgroundImage] forState:UIControlStateNormal]; 

} 

回答

6

試試這個: -

[btn2 setImage:btn1.currentImage forState:UIControlStateNormal]; 

currentBackgroundImageUIImage,您將其視爲NSString,這是錯誤的。

1

使用此

 UIImage *img = btn1.currentImage; 
     [btn2 setImage:img forState:UIControlStateNormal]; 
0

這是因爲你設置圖像按鈕btn1 setImage:和訪問的背景圖像btn1.currentBackgroundImage

因此改變你測試的代碼

-(void)Button2Method 
{ 
    [btn2 setImage:btn1.imageView.image forState:UIControlStateNormal]; 
}