Ios IBAction无法工作

Ios IBAction无法工作,ios,button,timer,ibaction,Ios,Button,Timer,Ibaction,如果用户一直单击按钮1一个或两个,则progress2.progress会在每次单击时不断增加/减少,progress1.progress会保持相同的值,直到用户停止单击为止。万一他肯定会输,如果他继续点击,在他停止点击之前什么也不会发生。我不希望这样,因为我想在确认他无法解决此问题后立即隐藏/禁用按钮。有办法解决吗 这是我的.m: #import "ViewController.h" @interface ViewController () @end @implementation

如果用户一直单击按钮1一个或两个,则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
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(BOOL)prefersStatusBarHidden{返回YES;}
-(无效)viewDidLoad
{
progress1.progress=arc4random()%11*0.1;
count1=0;
count2=0;
label1.hidden=否;
gameOver.hidden=是;
得分=0;
[超级视图下载];
;
}
//加载视图后,通常从nib执行任何其他设置。
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)监管机构{
如果(计时器1)
{
[计时器1失效];
定时器1=零;
}
如果(计时器4)
{
[计时器4失效];
timer4=nil;
}
timer4=[NSTimer scheduledTimerWithTimeInterval:1.5目标:自选择器:@selector(Conditor)userInfo:nil repeats:YES];
;}
-(无效)护发素{

如果(fabs(progress2.progress-progress1.progress)有两种方法可以确保当用户触摸按钮时,它不会做任何事情

你们的问题不清楚,所以我假设在你们按下按钮时,你们会调用(iAction)按钮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];
        }


    }

这实际上是一个编码问题

我相信你错过了一些对事物的理解。因此我将解释:

  • iAction-它是从视图发送到控制器的操作
  • IBOutlet-用于控制器控制视图
  • 在你的代码中,你得到了发送者(在正确编码时应该是只读的),并且你正在尝试设置它。我假设你需要定义一个新的IBOutlet来表示按钮,然后将它连接到你的故事板上,然后在这个函数中使其启用/禁用

    此外,一个好的做法是使用“真”和“假”而不是“是/否”


    希望这能有所帮助。

    谢谢你的回答,我想要第二个选项,但当我连接一个IBOutlet时,它会说:“button1”的本地声明隐藏实例变量,button2也一样。如果你在*.h中定义了UIButtons button1和button2,那么就从*.m文件中删除它的本地声明。也就是说,删除UIButton*button1=…并更改当我尝试这样做时,它说:“button1”的局部声明隐藏了实例变量,button2也是如此。用不同的名称调用变量:)错误消失了。如果用户将丢失,我将按钮设置为隐藏。但是,如果用户继续单击按钮1一个或两个,则progress2.progress会在每次单击时不断增加/减少,progress1.progress会保持相同的值,直到用户停止单击。如果用户继续单击,则不会发生任何事情,以防他肯定会丢失直到他停止点击。我不想这样,因为我想隐藏/禁用按钮来解决这个问题。有什么办法解决吗?这是一个不同的问题,如果没有看到所有的代码,很难回答。如果这个答案对你有帮助,请上传。如果你发布了所有相关的代码,我会看一看。请检查编辑后的代码,然后返回当你可以的时候告诉我。我添加了完整的代码。问候。
    - (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];
            }
    
    
        }