2012-05-10 90 views

回答

1
@interface XXX: UIViewController // whatever 
{ 
    BOOL btnImageState; 
} 

// etc. 

@implementation XXX 

- (id)init 
{ 
    if ((self = [super init])) 
    { 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal]; 
     [btn setFrame:CGRectMake(x, y, w, h)]; 
     [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.view addSubview:btn]; 
    } 
    return self; 
} 

- (void)click:(UIButton *)btn 
{ 
    if (btnImageState) 
     [btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal]; 
    else 
     [btn setImage:[UIImage imageNamed:@"unfollow.png"] forState:UIControlStateNormal]; 

    btnImageState = !btnImageState; 
} 

@end 
0

這裏我以編程方式做了。希望這會幫助你。

-(IBAction) pause:(id)sender 
{ 
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button1 addTarget:self 
      action:@selector(aMethod) 
forControlEvents:UIControlEventTouchDown]; 
UIImage *buttonImage = [UIImage imageNamed:@"buttonImage.png"]; 
[button1 setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
button1.frame = CGRectMake(208, 380.0, 60.0, 60.0); 
[self.view addSubview:button1]; 
} 
- (IBAction) aMethod 
{ 
button1.hidden=YES; 

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button1 addTarget:self 
      action:@selector(pause:) 
    forControlEvents:UIControlEventTouchDown]; 
UIImage *buttonImage = [UIImage imageNamed:@"Button2Image.png"]; 
[button1 setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
//[button1 setTitle:nil forState:UIControlStateNormal]; 
button1.frame = CGRectMake(208, 380.0, 60.0, 60.0); 
[self.view addSubview:button1]; 
} 
1

如果要使用按鈕作爲切換按鈕而不是使用標誌(BOOL)變量並根據您的要求進行設置。但如果你設置按鈕圖像不止一次,比使用它我顯示在下面。

-(IBAction)buttonPress:(id)sender 
{ 

    if([sender tag] == 1) 
     { 
      [yourbutton setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; 
      btnSegment1.tag = 2;// Here i reset tag. you can set tag according your requirements so you can use button next for next click as you want 

     } 
     else if([sender tag] == 2) 
     { 
      [yourbutton setBackgroundImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal]; 
      yourbutton.tag = 3;// Here i reset tag. you can set tag according your requirements so you can use button next for next click as you want 

     } 
    // and so on..... 

} 

希望,這將幫助你......

0
static int tapCount = 0; 
- (IBAction) buttonTapped :(id)sender { 
    tapCount++; 

    NSString *imageName = [NSString stringWithFormat:@"image%d.png",tapCount]; 
    [yourButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 
} 

你想怎麼加載具有什麼名字圖像和圖像的數量將取決於你。因此你可以自定義。