按钮不会禁用IOS目标C
我在给一个按钮编码,当它被按下的时候,它会发射一颗子弹。然而,我 我正试图禁用这个按钮,直到我想要它被使用。这是我的代码,它不起作用:按钮不会禁用IOS目标C,ios,objective-c,button,uibutton,Ios,Objective C,Button,Uibutton,我在给一个按钮编码,当它被按下的时候,它会发射一颗子弹。然而,我 我正试图禁用这个按钮,直到我想要它被使用。这是我的代码,它不起作用: #import "GameViewController.h" @interface GameViewController () @end @implementation GameViewController //What happens when shoot button is pressed -(IBAction)Shoot:(id)sender{
#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
Put
shot.enabled=否
进入-()viewDidLoad
函数。现在,在按下按钮之前,您不会告诉按钮被禁用
此外,您应该养成在objective-c中变量和函数名称中不大写第一个单词的第一个字母的习惯,例如,您可以命名字符串
myString
或按下的函数-(void)按钮。您通常只将类名的第一个单词大写,例如MyCustomObject
动画正在发生吗?什么叫“它不工作”?在这个短语中没有任何信息。我把shot.enabled=no;确实加载到视图中,但现在我无法在shot方法中使用if语句覆盖它。。谢谢你的帮助!我假设你有你的射击方法链接到你的射击按钮?如果是这种情况,您将无法从该方法重新启用它。可能添加shot.enabled=YES代码>到一个就绪方法的末尾,以便在应用程序通过这些方法后启用该按钮。