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

发布于 2024-12-10 20:02:25 字数 91 浏览 0 评论 0原文

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

提前致谢...

I have a button on which i want to set multiple event. When user touch button first time then i have change background image and when user again touch button then reset first image. How do that with button?

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧城烟雨 2024-12-17 20:02:25

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

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

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

You don't have to have multiple events, just call one function and in this function change the background (having some kind of indicator which background to display -> i here)

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

  if (i == 0)
    // first background
  else 
    // second background    
}
不必你懂 2024-12-17 20:02:25

尝试以下操作

    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 
       }
   }

Try the following

    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 
       }
   }
酷到爆炸 2024-12-17 20:02:25
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];
    }

}
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];
    }

}
夜巴黎 2024-12-17 20:02:25
- (void)changeBG {

  BOOL flag = TRUE;

  if (flag == TRUE) {

    // first background
    flag = FLASE;

  } else {

     // second background    
     flag = TRUE;
   }
}

在您的buttonTouchUPInside 方法中调用上述方法。

- (void)changeBG {

  BOOL flag = TRUE;

  if (flag == TRUE) {

    // first background
    flag = FLASE;

  } else {

     // second background    
     flag = TRUE;
   }
}

Call above method in your buttonTouchUPInside method.

心作怪 2024-12-17 20:02:25
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;
    }
}
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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文