您好亲爱的Stack Overflow会员; 我创建了很多UIButtons并将它们连接到IBOutlets,但我还将IBAction连接到所有这些UIButton。 但其他代码包括这些按钮,不起作用。代码被执行但不起作用。 Xcode也没有给出错误。 我该如何解决这个问题? 顺便说一句,这里是翻译; salise =分秒,saniye =秒,dakika =分钟。
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(我的班级);
#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
答案 0 :(得分:1)
每当您与按钮进行互动时,您正在进行GameController *gmc =[[GameController alloc]init];
。这是创建一个新的空,GameController
的实例,它不是具有您在屏幕上看到的实际按钮的实例。
你需要实际获得GameController
的'main'或true实例,它知道屏幕上的按钮。
通常,此代码应位于GameController
类而不是GameButtons
类......
另外,根据@Desdenova的推荐,请查看IBOutletCollection
。