【问题标题】:compilation problem on device not on simulator设备上的编译问题不在模拟器上
【发布时间】:2011-08-05 16:18:39
【问题描述】:

还有一些重构(为两个不同的类编写一个基类+其他一些东西),我的项目在 ipad 上编译失败,在模拟器上不能正常工作

/Users/.../PaintViewController.m:50: error: 'width' undeclared (first use in this function)
/Users/.../PaintViewController.m:59: error: 'backgroundView' undeclared (first use in this function)

当然,这些变量是声明的(在新的基类中),并且 2 个类继承自基类

任何想法为什么?我也导入了基类。

发生错误的类:

#import "PaintViewControllerBase.h"

@class PopupSaveDrawingController;


@interface PaintViewController : PaintViewControllerBase
{

MenuBarViewController       *menuBarView;

bool                        bBarIsOpened;    
bool                        bIsClosing;
bool                        bIsOpening;
float                       fBarY;

NSTimer                     *toggleTimer;

NSArray                     *toolBrushImgArray;// liste des textures de brosses    

PopupSaveDrawingController  *popupSaveDrawning;

}

基类:

@interface PaintViewControllerBase : UIViewController
{
// Handle Move              ///
CGPoint                     location;
CGPoint                     previousLocation;
BOOL                        firstTouch;

// Size                     ///
NSInteger                   width;
NSInteger                   height;

// Actions

UndoRedoManager             *undoManager;

toolType                    currentToolType;

// Brush
PaintBrush                  *brush;
PaintImage                  *image;

// image buffer             //////
NSMutableData               *data;

// GUI
PaintCanvas                 *backgroundView;
PaintCanvas                 *modeleView;
PaintCanvas                 *drawView;


}

编译失败的语句:

    width       = [PaintMenuViewController width]; // error here on ipad target only
    height      = [PaintMenuViewController height];// error here on ipad target only
    CGRect rect = CGRectMake(0,0,width,height);
    self.view   = [[UIView alloc] initWithFrame:rect];


    /**********************
     * IMAGEVIEW BACKGROUND
     **********************/
    backgroundView = [[PaintCanvas alloc] initWithFrame:rect]; // error here on ipad target only

如果我在每个变量之前添加self,问题似乎就消失了,例如:self.width = 1024,但我不想这样做(有很多东西要改变)

【问题讨论】:

  • 你应该发布基类的定义+编译失败的语句...
  • @sergio : 我添加了更多细节

标签: ios ipad compilation base-class


【解决方案1】:

编辑:

从您上次的评论看来,您首先缺少定义这些变量:

NSInteger width       = [PaintMenuViewController width];
...

如果这不正确,请在代码中添加更多上下文,否则将无法理解...

旧答案:

在语句中:

width       = [PaintMenuViewController width]; // error here on ipad target only
height      = [PaintMenuViewController height];// error here on ipad target only

您正在尝试错误地访问 ivars。

您应该先分配和初始化一个类,然后才能访问它的 ivars(成员变量)。

不清楚这段代码来自哪里(我的意思是来自哪个类/方法)。如果它位于其中一个视图控制器本身中,您只需执行以下操作:

width       = [self width]; 
height      = [self height];

否则,请指定代码上下文(类/方法)。

如果您需要类方法(即,您可以在分配它们之前对类本身调用的方法),您可以定义:

 @interface PaintViewControllerBase : UIViewController {
 }
 +(NSInteger)width;
 +(NSInteger)height;
 ...
 @end

这些方法可以返回您需要的值。

【讨论】:

  • 这个变量是 const +(const NSInteger) 宽度; +(const NSInteger) 高度
  • 我知道,但重点是:它们是普通的类变量(ivars),你需要一个对象(你的控制器)来访问它们。另请参阅我的下一个编辑...
  • 如果我写 width = 1024; 问题也是一样的;高度 = 768;
  • 如果我在每个变量之前添加self,问题似乎就消失了,例如:self.width = 1024,但我不想这样做(有很多东西要改变)
  • 您能解释一下这些变量的用途以及该方法的位置吗?
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 2014-01-15
  • 1970-01-01
相关资源
最近更新 更多