2014-03-05 46 views
0

我設置了一個表與我有這樣的代碼:有沒有在UIButtton本地進行操作的方法?

UIButton *done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)]; 
[done setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal]; 
[done addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside]; 
[cell addSubview:done]; 
return cell; 

這並不怎麼一回事,因爲此按鈕的工作只不過是在這個範圍內聲明,我想簡單地改變其對這個圖片的點擊

+1

不顧原來的問題(其他人已經給了一個很好的答案)...如果cell是'UITableViewCell',請將cell.contentView作爲子視圖添加到單元格中。 :-) –

回答

1

更改done:方法中的圖像。

- (void)done:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 

    [button setImage:[UIImage imageNamed:@"yourImage.png"] 
      forState:UIControlStateNormal]; 
} 
0

你做的方法是什麼樣子? 我認爲如果你添加一個sender參數給方法,你可以訪問按鈕。

- (void)done:(id)sender 
{ 
    UIButton *button = (UIButton*)sender; 
    [button doStuff]; 
} 
0

您必須申報ivar或私人/公共財產。所以,你可以從任何地方改變自己的形象,例如:

@interface ViewController() 

@property UIButton *done; 

@end 


@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)]; 
    [self.done setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal]; 
    [self.done addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:self.done]; 
} 

- (void)done:(UIButton *)sender 
{ 
    //Do something.. like change image 

    [self.done setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; 
} 

@end 

或者你聲明發件人爲UIButton的,就像這樣:

- (void)done:(id)sender 
{ 
    UIButton *done = (UIButton *)sender; 

    //Do something with the button 
} 
相關問題