如何在单个按钮上应用多个事件?

时间:2011-10-19 06:38:19

标签: iphone uibutton

我有一个按钮,我想设置多个事件。当用户第一次触摸按钮时,我有更改背景图像,当用户再次触摸按钮然后重置第一张图像。怎么用按钮?

提前致谢...

5 个答案:

答案 0 :(得分:0)

您不必拥有多个事件,只需调用一个函数,并在此函数中更改背景(具有某种指示器,显示哪个背景 - > i此处)

- (void)changeBackground {
  i = (i+1)%2;

  if (i == 0)
    // first background
  else 
    // second background    
}

答案 1 :(得分:0)

尝试以下

    int i = 0;
   -(void) buttonPressed{
       i++;
       if(i == 1){
           //Here load the view which you want to load on first touch to a button
       }
       else{
           //Here load the other view 
       }
   }

答案 2 :(得分:0)

- (void)changeBG {

  BOOL flag = TRUE;

  if (flag == TRUE) {

    // first background
    flag = FLASE;

  } else {

     // second background    
     flag = TRUE;
   }
}

在buttonTouchUPInside方法中调用上面的方法。

答案 3 :(得分:0)

in Button action 
-(void)clickOnCheckButton:(id)sender
{
    UIButton *btn_tmp=sender;
    if(!(btn_tmp.selected))
    {
        //you can set your image
        [btn_tmp setSelected:YES];
    }
    else {
        //you can set your image
        [btn_tmp setSelected:NO];
    }

}

答案 4 :(得分:0)

try it

- (IBAction)buttonPressed:(id)sender{
    static int counter;
    if (counter == 0){
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"ONE" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }else if (counter == 1){
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"TWO" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }else if (counter == 2){
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"THREE" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    counter += 1;


    if (counter >2){
        counter = 0;
    }
}