0

I created lots of UIButtons and I connected them to the IBOutlets but also I connected a IBAction to all of these UIButtons.

But the other codes those include these buttons, doesn't work. Code gets executed but doesn't work. Xcode doesn't give an error also.

How can I fix this issue?

By the way here are the translations:

  • salise = split second
  • saniye = second
  • dakika = minute

GameController.m (View Controller);

`#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 (My Class);

#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
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
batiaktas
  • 21
  • 4

1 Answers1

1

Every time you go to interact with your buttons you are doing GameController *gmc =[[GameController alloc]init];. This is creating a new, empty, instance of GameController which isn't the one that has the actual buttons you see on screen.

You need to actually get the 'main' or true instance of GameController which knows about the buttons on screen.

Generally, this code should be in the GameController class rather than in the GameButtons class...

Also, as per the commend from @Desdenova check out IBOutletCollection.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thank you for your response, how can I get the 'main' instance? – batiaktas Jul 21 '14 at 11:43
  • It looks like `GameController` is calling `GameButtons` so either move the code into `GameController` or pass `self` as a parameter when you call (or, better, use `IBOutletCollection` and pass the array of buttons to act on). – Wain Jul 21 '14 at 11:50
  • Thank you guys it fixed the pressability but it still doesn't set the buttons background :) Is still there a problem with instances? – batiaktas Jul 21 '14 at 12:01
  • You added a parameter to each method? It looks like the same problem in each case. – Wain Jul 21 '14 at 12:19
  • also when I get the [sender currentTitle] by triggering the 'buttonGame' action, log gives the senders currentTitle as (null). What does it mean? I already connected the IBOutlets and UIButtons. – batiaktas Jul 21 '14 at 12:45
  • What is `currentTitle`? It isn't declared anywhere in your question. Log the sender, what is it? – Wain Jul 21 '14 at 12:47
  • I added it to my project now. Inspiredfrom here http://stackoverflow.com/questions/5858247/ios-one-ibaction-for-multiple-buttons – batiaktas Jul 21 '14 at 12:50
  • Do your buttons have text or images on them? Log the sender. – Wain Jul 21 '14 at 13:06
  • no they don't have any text or images on them. They have just the background images that I added by Interface Builder. – batiaktas Jul 21 '14 at 13:08
  • So there will be no `currentTitle` (read the documentation for what `currentTitle` returns). – Wain Jul 21 '14 at 13:09