ios - 在继承中传递变量 (Obj - C)

标签 ios objective-c inheritance

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

相关文章:

ios - 为什么在音频队列播放代码中调用 CFRunLoopRunInMode()?

ios - 在启动之前在拳头选项卡 View Controller 上加载数据

iphone - 顶部和底部具有透明渐变的 UITableView

objective-c - NSMutableArray as @property with readonly

c++ - 如何创建一个接口(interface)与 double 匹配但模板可以专门化的类?

java - 在Java中的父类(super class)方法中创建一个新的子类

ios - 标准Parse.com设置会发出警告“正在执行长时间运行的操作……”

ios - 访问 UITableViewController 单元格集合

objective-c - NSTableColumn 何时被释放?

ios - 子类中 self 和 super 之间的混淆