Button不会禁用IOS Objective C.

时间:2014-10-02 23:05:20

标签: ios objective-c button uibutton

我编写了一个按钮,按下该按钮可以射击射弹。但是,我 我试图禁用此按钮,直到我想要它被使用。这是我的代码,它不起作用:

#import "GameViewController.h"

@interface GameViewController ()

@end

@implementation GameViewController

//What happens when shoot button is pressed

-(IBAction)Shoot:(id)sender{

    Shoot.enabled = NO;

    //Step 1: upload image Step 2: Load image into image view Step 3: set coordinates and size
    UIImage * Bulletimage= [UIImage imageNamed:@"bullet.png"];
    UIImageView * Bullet = [[UIImageView alloc] initWithImage:Bulletimage];
    [Bullet setFrame:CGRectMake(130,430,35,35)];
    [self.view addSubview:Bullet];

    //Creating Animation
    [UIView animateWithDuration:0.7 delay:0 options: UIViewAnimationOptionAllowAnimatedContent animations:^{ [Bullet setFrame:
                                                                                                              CGRectMake (130, 20, 35, 35)];}
                     completion:^(BOOL finished) {Bullet.hidden = YES;}];

}


//what happens when ready button is pressed
-(IBAction)Ready:(id)sender{

    //button disapears
    Ready.hidden = YES;

}

-(IBAction)Ready2:(id)sender{

    //button disapears
    Ready2.hidden = YES;

    if (Ready.hidden= YES, Ready2.hidden = YES)
    {
        //declaring images for animation
        Countdown.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"3.png"],
                                     [UIImage imageNamed:@"2.png"],
                                     [UIImage imageNamed:@"1.png"],
                                     nil];

        [Countdown setAnimationRepeatCount:1];
        Countdown.AnimationDuration = 3;
        [Countdown startAnimating];

       //end of loop
         }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

1 个答案:

答案 0 :(得分:2)

Shoot.enabled = NO;放入-()viewDidLoad功能。现在你没有告诉按钮被禁用,直到按下按钮。

另外,你应该养成不在objective-c中的变量和函数名中大写第一个单词的第一个字母的习惯,例如你可以命名一个字符串myString或一个函数{{1} }。您通常只会将类名的第一个单词大写,如-(void)buttonPressed