问题描述
我有一个自定义 UIVIewController,它是其他控制器的基类,并且有一个自定义 UIView 变量的实例,该变量由继承的类访问.
I have an custom UIVIewController that is the base class for other controllers and has an instance of a custom UIView variable that is accessed by inherited the classes.
BaseViewController.h
BaseViewController.h
@interface BaseViewController : UIViewController {
UIView *_vwHeader;
}
@end
BaseViewController.m
BaseViewController.m
#import "BaseViewController.h"
@implementation BaseViewController
-(void)loadView {
[super loadView];
_vwHeader = [[UIView alloc] init];
}
@end
CustomViewController.h
CustomViewController.h
#import "BaseViewController.h"
@interface CustomViewController : BaseViewController
@end
CustomViewController.m
CustomViewController.m
#import "CustomViewController.h"
@implementation CustomViewController
- (void)loadView
{
[super loadView];
[_vwHeader setHidden:NO];
}
@end
问题是,当我在模拟器上运行它时,一切正常,但是当我切换到设备时,我在 [_vwHeader setHidden:NO];
行出现错误: '_vwHeader' 未声明(此函数中首次使用)
The problem is that when I am running it on the simulator everything works perfectly fine, but when I change to the device I have an error on the [_vwHeader setHidden:NO];
line which says: '_vwHeader' undeclared (first use in this function)
我已经尝试过:
- 注释这行代码,但是它在另一个类中以相同的方式使用基类中的变量给我一个错误(它一次只返回一个错误),所以它似乎不是一个特定的错误在视图或控制器类中,因为错误发生在具有不同类型的其他类中,例如
UIView
和NSObject
类型 - 更改目标编译器配置,例如:架构(所有这些)、基础 sdk(4.0 以上)没有改变任何东西
什么似乎解决了问题,但不完全
What seem to solve the problem, but not completely
- 为
_vwHeader
创建一个属性并通过self._vwHeader
或super._vwHeader
访问它似乎可行,但必须创建一个属性仅仅访问一个变量并不会让我感到舒服,特别是因为我必须在我的项目中为相同情况下的所有变量都这样做. - 更改了 C/C++ 编译器版本:使用
Apple LLVM Compiler 2.1
使编译错误消失,但在项目中使用其他库时会出现一系列其他问题.因此,这不是最终的解决方案,但可能是问题所在的线索.
- Creating a property for
_vwHeader
and accessing it byself._vwHeader
orsuper._vwHeader
seems to work, but having to create a property just to access a variable does not make me confortable, specially because I would have to do it for all variables in the same situation inside my project. - changed C/C++ compiler version: using
Apple LLVM Compiler 2.1
makes the compilation error goes away, but gives a bunch of other problems with other libraries being used in the project. So, it is not a definitive solution, but might be a clue of what the problem is.
我试图创建另一个不是指针的变量,一个BOOL
而不是UIView *
然后在继承的类中使用它:问题也出现
I tried to create another variable that is not a pointer, a BOOL
instead of the UIView *
and then used it in the inherited class: the problem also occurs
编辑(2):
我的任何类中都没有任何属性,但仍然出现错误.我刚刚添加了测试海豚的属性,以查看父类中的属性是否导致了相同的行为,但显然没有.还有一点很奇怪,当我在变量中得到错误时,我检查了我的智能感知并找到了它......
I have no properties whatsoever in any of my classes and I still get the error.I just added the properties for test porpouses, to see if a property in a parent class caused the same behaviour, and apparently it doesn't.Something that is also weird is that when I get the error in the variable, I checked with my intellisense and it finds it...
推荐答案
为了在任何对象中引用一个实例变量而不是 self
,包括 super
,你必须使用结构指针运算符 (->
).实例变量的默认范围是受保护的,这意味着它只能在定义它的类或该类的子类中访问.由于 CustomViewController
是 BaseViewController
的子类,所以这个范围足以使用 self->_vwHeader
访问变量,但是如果第二个类你试图从不是子类执行此操作,您还需要将范围更改为 @public
或 @package
.
In order to refer to an instance variable within any object other than self
, including super
, you must use the structure pointer operator (->
). The default scope of an instance variable is protected, which means it can only be accessed within the class it is defined in or a subclass of that class. Since CustomViewController
is a subclass of BaseViewController
, this scope is sufficient to access the variable using self->_vwHeader
, but if the second class you were trying to do this from is not a subclass you will also need to change the scope to either @public
or @package
.
总而言之,将您的方法调用更改为:
In summary, change your method call to:
[self->_vwHeader setHidden:NO];
它应该适用于基本视图控制器的任何子类.
and it should work for any subclasses of the base view controller.
这篇关于“未声明的变量"编译到 iOS 设备时出错,但不适用于模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!