我在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


我无法在派生类中使用以上声明的变量!

最佳答案

您所有的变量都在您的类的私有@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;
}

10-08 05:45
查看更多