我在 Obj-C 中开发一个项目,其中有一个基类 (ViewController) 和一个派生类 (MultiPlayer)。
现在我已经在基类中声明了某些变量和属性。
我的属性正在从派生类访问,但我无法访问变量(int、char 和 bool 类型)。
我是 Obj-C 的新手,所以我不知道哪里出了问题。
我使用了 C 和 C++ 中使用的数据类型。
在 Obj-C 中有一些特定的方法来声明变量吗?
如果是,怎么做?
这是我的文件
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet UIImageView* backGroungImage;
@property (strong,nonatomic) IBOutlet UIImageView *blockView1;
@property (strong,nonatomic) IBOutlet UIImageView *blockView2;
@property (strong,nonatomic) IBOutlet UIImageView *blockView3;
@property (strong,nonatomic) IBOutlet UIImageView *blockView4;
@property (strong,nonatomic) IBOutlet UIImageView *blockView5;
@property (strong,nonatomic) IBOutlet UIImageView *blockView6;
@property (strong,nonatomic) IBOutlet UIImageView *blockView7;
@property (strong,nonatomic) IBOutlet UIImageView *blockView8;
@property (strong,nonatomic) IBOutlet UIImageView *blockView9;
@property (strong,nonatomic) UIImage *x;
@property (strong,nonatomic) UIImage *O;
@property (strong,nonatomic) IBOutlet UIImageView* back1;
@property (strong,nonatomic) IBOutlet UIImageView* back2;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
int chooseTheBackground = 0;
int movesToDecideXorO = 0;
int winningArrayX[3];
int winningArrayO[3];
int blocksTotal[9] = {8,3,4,1,5,9,6,7,2};
int checkIfContentInBlocks[9] = {0,0,0,0,0,0,0,0,0};
char determineContentInBlocks[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
bool player1Win = false;
bool player2Win = false;
bool playerWin = false;
bool computerWin = false;
- (void)viewDidLoad
{
[super viewDidLoad];
if(chooseTheBackground==0)
{
UIImage* backImage = [UIImage imageNamed:@"MainBack1.png"];
_backGroungImage.image=backImage;
}
if(chooseTheBackground==1)
{
UIImage* backImage = [UIImage imageNamed:@"MainBack2.png"];
_backGroungImage.image=backImage;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我无法在我的派生类中使用上面声明的变量!
最佳答案
你所有的变量都在你的类的私有(private) @implementation
中,如果你想让它们公开访问,把它们放在你的 .h 文件中。
@interface ViewController : UIViewController {
int chooseTheBackground;
int movesToDecideXorO;
int winningArrayX[3];
int winningArrayO[3];
int blocksTotal[9];
int checkIfContentInBlocks[9];
char determineContentInBlocks[9];
bool player1Win;
bool player2Win;
bool playerWin;
bool computerWin;
}
关于ios - 在继承中传递变量 (Obj - C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473720/