【问题标题】:How do I fix this code that executes but does not work?如何修复此执行但不起作用的代码?
【发布时间】:2014-07-21 11:18:54
【问题描述】:

我创建了许多 UIButton,并将它们连接到 IBOutlets,但我还将 IBAction 连接到所有这些 UIButton。

但包含这些按钮的其他代码不起作用。代码被执行但不起作用。 Xcode 也不会报错。

我该如何解决这个问题?

顺便说一句,这里是翻译:

  • salise = 瞬间
  • saniye = 第二个
  • dakika = 分钟

GameController.m(视图控制器);

`#import "GameController.h"
#import "GameButtons.h

@class UIButton;
@interface GameController ()

@end
@implementation GameController
@synthesize firstlabel, timelabel,running,selected;
@synthesize button1,button2,button3,button4,button5,button6,button7,button8,button9,button10;
@synthesize button11,button12,button13,button14,button15,button16,button17,button18,button19,button20;
@synthesize button21,button22,button23,button24,button25,button26,button27,button28,button29,button30;
@synthesize button31,button32,button33,button34,button35,button36,button37,button38,button39,button40;
@synthesize button41,button42,button43,button44,button45,button46,button47,button48,button49,startButton;
int salise = 0;
int saniye = 0;
int dakika = 0;
int down = 3;
bool countdownOnline;
-(IBAction)buttonGame:(id)sender{
    selected=sender;
    if ([selected.currentBackgroundImage isEqual:[UIImage imageNamed:@"buttonRed.png"]]) {
        int random = arc4random()%(50-0);
        [[GameButtons selectedButton:random] setBackgroundImage:[UIImage imageNamed:@"buttonRed.png"] forState:UIControlStateNormal];
    }
    else{
        running=NO;
    }

}
-(IBAction)buttonStart:(id)sender{
    running = YES;
    countdownOnline=YES;
    [timelabel setText:[NSString stringWithFormat:@"%d", down]];
    down--;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdown:) userInfo:nil repeats:YES];
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
    [firstlabel setText:@""];
    startButton.enabled=NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)updateTime:(NSTimer*)timer{
    if (countdownOnline==NO){
    NSString *fulltime;
    NSString *strSalise;
    salise++;
    if (salise==100) {
        salise=0;
        saniye++;
        if (saniye==60) {
            saniye=0;
            dakika++;
        }
        strSalise = [NSString stringWithFormat:@"%d",salise];
    }
    else if (salise <10) {
        strSalise = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%d",salise]];
    }
    else{
        strSalise = [NSString stringWithFormat:@"%d",salise];
    }
    NSLog(@"time done");
    fulltime = [[NSString stringWithFormat:@"%d",dakika] stringByAppendingString:@":"];
    fulltime = [fulltime stringByAppendingString:[NSString stringWithFormat:@"%d",saniye]];
    fulltime = [fulltime stringByAppendingString:@":"];
    fulltime = [fulltime stringByAppendingString:strSalise];
    [timelabel setText:fulltime];
    }
    else if (running==NO){
        [timer invalidate];
    }
    else{}
}
-(void)countdown:(NSTimer*)timer{
    if (down>0) {
        [timelabel setText:[NSString stringWithFormat:@"%d",down]];
        down--;
    }
    else if (down==0){
        [timelabel setText:@"GO!"];
        down--;
    }
    else if(down<0){
        [timer invalidate];
        countdownOnline=NO;
        int random = (arc4random()%1)+49;
        [[GameButtons selectedButton:random] setBackgroundImage:[UIImage imageNamed:@"buttonRed.png"] forState:UIControlStateNormal];
        [GameButtons makebuttonsPressable];
    }
    NSLog(@"Countdown done.");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [GameButtons makebuttonsUnpressable];
}

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

GameButtons.m(我的班级);

#import "GameButtons.h"
#import "GameController.h"

@implementation GameButtons
+(UIButton*)selectedButton:(int)intran{
    UIButton *button;
    GameController *gmc =[[GameController alloc]init];
    if (intran==0){
        button = gmc.button1;
    }
    else if (intran==1){
        button = gmc.button2;
    }
    else if (intran==2){
        button = gmc.button3;
    }
    else if (intran==3){
        button = gmc.button4;
    }
    else if (intran==4){
        button = gmc.button5;
    }
    else if (intran==5){
        button = gmc.button6;
    }
    else if (intran==6){
        button = gmc.button7;
    }
    else if (intran==7){
        button = gmc.button8;
    }
    else if (intran==8){
        button = gmc.button9;
    }
    else if (intran==9){
        button = gmc.button10;
    }
    else if (intran==10){
        button = gmc.button11;
    }
    else if (intran==11){
        button = gmc.button12;
    }
    else if (intran==12){
        button = gmc.button13;
    }
    else if (intran==13){
        button = gmc.button14;
    }
    else if (intran==14){
        button = gmc.button15;
    }
    else if (intran==15){
        button = gmc.button16;
    }
    else if (intran==16){
        button = gmc.button17;
    }
    else if (intran==17){
        button = gmc.button18;
    }
    else if (intran==18){
        button =gmc.button19;
    }
    else if (intran==19){
        button = gmc.button20;
    }
    else if (intran==20){
        button = gmc.button21;
    }
    else if (intran==21){
        button = gmc.button22;
    }
    else if (intran==22){
        button = gmc.button23;
    }
    else if (intran==23){
        button = gmc.button24;
    }
    else if (intran==24){
        button = gmc.button25;
    }
    else if (intran==25){
        button = gmc.button26;
    }
    else if (intran==26){
        button =gmc.button27;
    }
    else if (intran==27){
       button = gmc.button28;
    }
    else if (intran==28){
        button = gmc.button29;
    }
    else if (intran==29){
        button = gmc.button30;
    }
    else if (intran==30){
        button = gmc.button31;
    }
    else if (intran==31){
        button = gmc.button32;
    }
    else if (intran==32){
        button = gmc.button33;
    }
    else if (intran==33){
        button = gmc.button34;
    }
    else if (intran==34){
        button = gmc.button35;
    }
    else if (intran==35){
        button = gmc.button36;
    }
    else if (intran==36){
        button = gmc.button37;
    }
    else if (intran==37){
        button = gmc.button38;
    }
    else if (intran==38){
        button = gmc.button39;
    }
    else if (intran==39){
        button = gmc.button40;
    }
    else if (intran==40){
        button = gmc.button41;
    }
    else if (intran==41){
        button = gmc.button42;
    }
    else if (intran==42){
        button = gmc.button43;
    }
    else if (intran==43){
        button = gmc.button44;
    }
    else if (intran==44){
        button = gmc.button45;
    }
    else if (intran==45){
        button = gmc.button46;
    }
    else if (intran==46){
        button = gmc.button47;
    }
    else if (intran==47){
        button = gmc.button48;
    }
    else{
        button = gmc.button49;
    }
    return button;
}
+(void)makebuttonsUnpressable{
    GameController *gmc =[[GameController alloc]init];
    gmc.button1.enabled=NO;
    gmc.button2.enabled=NO;
    gmc.button3.enabled=NO;
    gmc.button4.enabled=NO;
    gmc.button5.enabled=NO;
    gmc.button6.enabled=NO;
    gmc.button7.enabled=NO;
    gmc.button8.enabled=NO;
    gmc.button9.enabled=NO;
    gmc.button10.enabled=NO;
    gmc.button11.enabled=NO;
    gmc.button12.enabled=NO;
    gmc.button13.enabled=NO;
    gmc.button14.enabled=NO;
    gmc.button15.enabled=NO;
    gmc.button16.enabled=NO;
    gmc.button17.enabled=NO;
    gmc.button18.enabled=NO;
    gmc.button19.enabled=NO;
    gmc.button20.enabled=NO;
    gmc.button21.enabled=NO;
    gmc.button22.enabled=NO;
    gmc.button23.enabled=NO;
    gmc.button24.enabled=NO;
    gmc.button25.enabled=NO;
    gmc.button26.enabled=NO;
    gmc.button27.enabled=NO;
    gmc.button28.enabled=NO;
    gmc.button29.enabled=NO;
    gmc.button30.enabled=NO;
    gmc.button31.enabled=NO;
    gmc.button32.enabled=NO;
    gmc.button33.enabled=NO;
    gmc.button34.enabled=NO;
    gmc.button35.enabled=NO;
    gmc.button36.enabled=NO;
    gmc.button37.enabled=NO;
    gmc.button38.enabled=NO;
    gmc.button39.enabled=NO;
    gmc.button40.enabled=NO;
    gmc.button41.enabled=NO;
    gmc.button42.enabled=NO;
    gmc.button43.enabled=NO;
    gmc.button44.enabled=NO;
    gmc.button45.enabled=NO;
    gmc.button46.enabled=NO;
    gmc.button47.enabled=NO;
    gmc.button48.enabled=NO;
    gmc.button49.enabled=NO;
     NSLog(@"Buttons became unpressable");
}
+(void)makebuttonsPressable{
    GameController *gmc =[[GameController alloc]init];
    gmc.button1.enabled=YES;
    gmc.button2.enabled=YES;
    gmc.button3.enabled=YES;
    gmc.button4.enabled=YES;
    gmc.button5.enabled=YES;
    gmc.button6.enabled=YES;
    gmc.button7.enabled=YES;
    gmc.button8.enabled=YES;
    gmc.button9.enabled=YES;
    gmc.button10.enabled=YES;
    gmc.button11.enabled=YES;
    gmc.button12.enabled=YES;
    gmc.button13.enabled=YES;
    gmc.button14.enabled=YES;
    gmc.button15.enabled=YES;
    gmc.button16.enabled=YES;
    gmc.button17.enabled=YES;
    gmc.button18.enabled=YES;
    gmc.button19.enabled=YES;
    gmc.button20.enabled=YES;
    gmc.button21.enabled=YES;
    gmc.button22.enabled=YES;
    gmc.button23.enabled=YES;
    gmc.button24.enabled=YES;
    gmc.button25.enabled=YES;
    gmc.button26.enabled=YES;
    gmc.button27.enabled=YES;
    gmc.button28.enabled=YES;
    gmc.button29.enabled=YES;
    gmc.button30.enabled=YES;
    gmc.button31.enabled=YES;
    gmc.button32.enabled=YES;
    gmc.button33.enabled=YES;
    gmc.button34.enabled=YES;
    gmc.button35.enabled=YES;
    gmc.button36.enabled=YES;
    gmc.button37.enabled=YES;
    gmc.button38.enabled=YES;
    gmc.button39.enabled=YES;
    gmc.button40.enabled=YES;
    gmc.button41.enabled=YES;
    gmc.button42.enabled=YES;
    gmc.button43.enabled=YES;
    gmc.button44.enabled=YES;
    gmc.button45.enabled=YES;
    gmc.button46.enabled=YES;
    gmc.button47.enabled=YES;
    gmc.button48.enabled=YES;
    gmc.button49.enabled=YES;
    NSLog(@"Buttons became pressable");
}
@end

【问题讨论】:

  • 天啊! 那是什么可怕的代码?! 有那么严重吗?我们很幸运,你不应该处理 200 个按钮......
  • Erm..... 那么什么不起作用?
  • 我是新手,不懂精灵套件,所以我必须用自己的方式制作游戏
  • 我没有设置所选按钮的背景,也没有使按钮无法按下或按下。 @CW0007007
  • 可能与您的问题无关,但请阅读 IBOutletCollectionObjective-C Fast Enumeration

标签: ios objective-c oop sprite-kit


【解决方案1】:

每次您与按钮进行交互时,您都在做GameController *gmc =[[GameController alloc]init];。这将创建一个新的 实例 GameController,它不是您在屏幕上看到的实际按钮。

您需要实际获取知道屏幕上按钮的GameController 的“主”或真实实例。

一般来说,这段代码应该在GameController类而不是GameButtons类...

另外,根据@Desdenova 的推荐,请查看IBOutletCollection

【讨论】:

  • 感谢您的回复,如何获取“主”实例?
  • 看起来GameController 正在调用GameButtons,所以要么将代码移动到GameController,要么在调用时将self 作为参数传递(或者,更好的是,使用IBOutletCollection 并传递要操作的按钮数组)。
  • 谢谢你们,它修复了可按压性,但它仍然没有设置按钮背景:) 实例仍然有问题吗?
  • 你给每个方法加了一个参数?在每种情况下看起来都是相同的问题。
  • 当我通过触发“buttonGame”操作获得 [sender currentTitle] 时,日志将发送者 currentTitle 设为(null)。这是什么意思?我已经连接了 IBOutlets 和 UIButtons。
猜你喜欢
  • 2019-06-12
  • 1970-01-01
  • 2022-08-17
  • 2022-12-16
  • 1970-01-01
  • 2014-01-25
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多