IBAction禁用不起作用

时间:2014-08-03 12:04:42

标签: ios button timer ibaction

如果用户持续点击button1上的一个或两个,则progress2.progress在每次单击时保持增加/减少,progress1.progress保持相同的值,直到用户停止点击。万一他肯定会失败,如果他一直没有点击任何事情,直到他停止点击。我不希望它是这样的,因为我想在他们确认他失去修复此问题后立即隐藏/禁用按钮。有什么方法可以解决这个问题吗?

这是我的.m:

#import "ViewController.h"



@interface ViewController ()


@end

@implementation ViewController






- (BOOL)prefersStatusBarHidden { return YES; }

- (void)viewDidLoad

{

    progress1.progress=arc4random() % 11 * 0.1;

    count1=0;
    count2=0;
    label1.hidden = NO;
    gameOver.hidden = YES;
    score=0;


    [super viewDidLoad];


    ;
}




    // Do any additional setup after loading the view, typically from a nib.



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)regulator{
   if(timer1)
    {
        [timer1 invalidate];
        timer1 = nil;
    }

    if(timer4)
    {
        [timer4 invalidate];
        timer4 = nil;
    }

    timer4 =[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(conditioner) userInfo:nil repeats:YES];




       ;}


-(void)conditioner {
        if (fabs(progress2.progress-progress1.progress)<=0.25            )

        {
            score=score+1;
            scorenumber.text= [NSString stringWithFormat:@"%i",score];
            [self newGame];
            ;
        } else{

            stop1=YES;
            stop2=YES;
            gameOver.hidden=NO;
            stick.hidden=YES;
            bg.hidden=YES;
            progress1.hidden=YES;
            progress2.hidden=YES;
            supply.hidden=YES;
            demand.hidden=YES;
        }}


-(void)newGame{




       progress1.progress=arc4random() % 11 * 0.1;}













- (IBAction)start:(UIButton *)sender {
    progress2.progress=arc4random() % 11 * 0.1;




   if(timer4)
    {
        [timer4 invalidate];
        timer4 = nil;


   timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(regulator) userInfo:nil repeats:YES];



      [self regulator];
    stop1=NO;
    stop2=NO;
    label1.hidden=YES;

    UIButton *button1 = (UIButton *)sender;
    button1.enabled = NO;

    UIButton *button2 = (UIButton *)sender;
    button2.enabled = NO;



}






- (IBAction)button1:(UIButton *)sender {


    if(stop1==YES){button12.hidden = TRUE;}
    progress2.progress=progress2.progress-0.05;



                                       ;





    [self regulator];



    count2=0;
    count1 = count1 +1;




   }



- (IBAction)button2:(UIButton *)sender {

    [self regulator];

    progress2.progress=progress2.progress+0.05;

    if(stop2==YES){button22.hidden = TRUE;}



    count1 =0;
    count2 = count2+1;

   }



@end

和我的.h:

#import <UIKit/UIKit.h>

int count1;
int count2;
int score;
void *regulator;
void *newGame;
void *conditioner;
BOOL stop1;
BOOL stop2;
void *firstLaunch;



@interface ViewController : UIViewController{
    IBOutlet UILabel *scorenumber;
    IBOutlet UIImageView *stick;
    IBOutlet UILabel *label1;
    IBOutlet UIImageView *bg;
    IBOutlet UILabel *supply;

    IBOutlet UILabel *demand;

    IBOutlet UILabel *gameOver;
    IBOutlet UIProgressView *progress1;

    IBOutlet UIProgressView *progress2;


    IBOutlet UIButton *button12;

    IBOutlet UIButton *button22;
    NSTimer *timer1;

    NSTimer *timer2;
    NSTimer *timer3;
    NSTimer *timer4;

   }

- (IBAction)button1:(UIButton *)sender;
- (IBAction)button2:(UIButton *)sender;













    @end

非常感谢任何帮助或信息。我用完整的代码编辑了我的问题,以进一步解释我面临的问题。问候。

2 个答案:

答案 0 :(得分:0)

有几种方法可以解决这个问题,以确保当用户触摸按钮时它没有做任何事情。

从您的问题中无法清除,所以我假设按下您按下的按钮(IBAction)按钮1。所以试试

- (IBAction)button1:(UIButton *)sender 
{


    if(stop1==YES)
    {
       //game has stopped so don't do anything
    }
    else
    {

      progress2.progress=progress2.progress-0.05;
      [self regulator];
    }


}

如果您想隐藏它以便用户无法尝试此操作

- (IBAction)button1:(UIButton *)sender 
    {


        if(stop1==YES)
        {
           //game has stopped so hide this button
           //assuming you have connected an IBOutlet to your button1
           button1.hidden = YES;
        }
        else
        {
          button1.hidden = NO;
          progress2.progress=progress2.progress-0.05;
          [self regulator];
        }


    }

答案 1 :(得分:0)

这实际上是一个编码问题。 MVC基础知识。

我相信你会错过一些对事物的理解。所以我会解释一下:

  1. IBAction - 这是从视图发送到控制器的动作。
  2. IBOutlet - 控制器控制视图。
  3. 在您的代码上,您将获得发件人(只有在编码时才能读取)并且您正在尝试设置它。我假设你需要定义一个新的IBOutlet来表示按钮然后将它连接到你的故事板上,然后在这个函数里面使它启用/禁用。

    另一个好的做法是使用“TRUE”和“FALSE”而不是“YES / NO”。

    希望这有帮助。