gpt4 book ai didi

iphone - 如何在实现文件中运行一个函数?

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:27 26 4
gpt4 key购买 nike

我正在 iPhone 上做一个项目,我是个初学者。我正在制作 Tic Tac Toe 应用程序,我想制作一个函数,将轮次从 'X' 更改为 'O'

所以我试着输入这个

void setTurn(){
SinglePlayer*SO=[[SinglePlayer alloc]init];
SO .turn =1;
if (SO.turn%2==0){
SO->turnLabel.text=@"O Turn";
}
else SO->turnLabel.text=@"X Turn";
SO.turn++;
}

SO 表示 SinglePlayerObject

无论我在实现的头部定义了什么

当我尝试在方法下键入此方法时 putSign();

-(IBAction)PutSign2:(id)sender{
if (turn%2 !=0){
label2.text=@"X";
}
else if (turn%2 ==0){
label2.text=@"O";
}
}

它显示错误。

既然我有使用C++的经验,那么如何才能运行与C++相同的方法呢?

这是头文件

#import <UIKit/UIKit.h>

@interface SinglePlayer : UIViewController{

IBOutlet UILabel*label1;
IBOutlet UILabel*label2;
IBOutlet UILabel*label3;
IBOutlet UILabel*label4;
IBOutlet UILabel*label5;
IBOutlet UILabel*label6;
IBOutlet UILabel*label7;
IBOutlet UILabel*label8;
IBOutlet UILabel*label9;

BOOL Player;

int turn;

IBOutlet UILabel*turnLabel;


}
@property (nonatomic)int turn;
@property (nonatomic) BOOL Player;
-(IBAction)PutSign1:(id)sender;
-(IBAction)PutSign2:(id)sender;
-(IBAction)PutSign3:(id)sender;
-(IBAction)PutSign4:(id)sender;
-(IBAction)PutSign5:(id)sender;
-(IBAction)PutSign6:(id)sender;
-(IBAction)PutSign7:(id)sender;
-(IBAction)PutSign8:(id)sender;
-(IBAction)PutSign9:(id)sender;
void setTurn();


@end

这是未完成的实现

@implementation SinglePlayer
//Player=true = X
//Player=False= O
@synthesize Player;
@synthesize turn;

void setTurn(){
SinglePlayer*SO=[[SinglePlayer alloc]init];

SO .turn =1;
if (SO.turn%2==0){
SO->turnLabel.text=@"O Turn";

}
else SO->turnLabel.text=@"X Turn";
SO.turn++;
}
//this code to put the sign in the button label:-
//[sender setTitle:@"X" forState:UIControlStateNormal];
-(IBAction)PutSign1:(id)sender{
if (turn%2 !=0){
label1.text=@"X";
}
else if (turn%2 ==0){
label1.text=@"O";
}
turnLabel.text=@"O Turn";
}
putSign();
-(IBAction)PutSign2:(id)sender{
if (turn%2 !=0){
label2.text=@"X";
}
else if (turn%2 ==0){
label2.text=@"O";

}
}
putSign();

-(IBAction)PutSign3:(id)sender{
if (turn%2 !=0){
label3.text=@"X";
}
else if (turn%2 ==0){
label3.text=@"O";

}
}
putSign();
-(IBAction)PutSign4:(id)sender{
//[sender setTitle:@"X" forState:UIControlStateNormal];
}

putSign();

最佳答案

名字集会搞砸了;)我知道这听起来无关紧要,但我不会低估“命名的力量”

代码变得更容易读/写


总之...你没有函数——你有属于对象的方法:它的 [OBJECT executeMyMethod:myParam];

关于iphone - 如何在实现文件中运行一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12453805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com