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
@interface BaseViewController : UIViewController {
UIView *_vwHeader;
}
@end
BaseViewController.m
#import "BaseViewController.h"
@implementation BaseViewController
-(void)loadView {
[super loadView];
_vwHeader = [[UIView alloc] init];
}
@end
CustomViewController.h
#import "BaseViewController.h"
@interface CustomViewController : BaseViewController
@end
CustomViewController.m
#import "CustomViewController.h"
@implementation CustomViewController
- (void)loadView
{
[super loadView];
[_vwHeader setHidden:NO];
}
@end
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 err开发者_运维问答or on the [_vwHeader setHidden:NO];
line which says: '_vwHeader' undeclared (first use in this function)
I already tried to do:
- Comment this line of code, but then it gives me an error in another class using a variable from the base class the same way (It only returns one error at a time), so it seems that it is not an specific error in the view or the controller class as the error occurs in other clases with different types, such as
UIView
andNSObject
types - Change target compiler configuration, such as: architectures (all of them), base sdk (all above 4.0) didn't change anything
What seem to solve the problem, but not completely
- 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.
EDIT:
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
EDIT (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...
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.
Do a clean and build, and also make sure you are not specifying a specific framework search path in the build settings. If you leave it empty you should get the correct libraries. well I don't know, should work.
BaseViewController.h
@interface BaseViewController : UIViewController {
UIView *_vwHeader;
}
@property(nonatomic,retain)UIView *_vwHeader;
@end
BaseViewController.m
@synthesize _vwHeader;
CustomViewController.m
#import "CustomViewController.h"
@implementation CustomViewController
- (void)loadView
{
[super loadView];
[self._vwHeader setHidden:NO];
}
@end
I faced similar problem as you. In my case the reason was (strangely!) wrong synthesization of properties in subclass.
Example:
In .h file of subclass you have following declaration
BOOL _flag;
...
@property (nonatomic, assign) BOOL flag;
while in you synthesize the property in the wrong way:
@synthesize flag;
instead of
@synthesize flag = _flag;
Strangely, the compiler does not complain about the wrong synthesization (the properties even work fine!), but raises an error, when I try to access protected fields declared in base class.
Detailed explanation
Here is what my code look like
I have base class (excerpt):
@interface BaseEditionModalController : NSObject
{
DataContext *_dataContext;
}
And I have subclass of it (excerpt):
@interface LocationModalController : BaseEditionModalController
{
MCLocation *_readLocation;
LocationCommModel *_oldLocationCommModel;
}
//This is MCLocation for reading only - from the main application context
@property (nonatomic, retain) MCLocation *readLocation;
@property (nonatomic, retain) LocationCommModel *oldLocationCommModel;
@end
And in the LocationModalController.m I have following wrong declarations:
@implementation LocationModalController
@synthesize readLocation;
@synthesize oldLocationCommModel;
Trying to access _dataContext in LocationModalController produced the error that _dataContext is undeclared.
Changing the synthesization of properties to:
@implementation LocationModalController
@synthesize readLocation = _readLocation;
@synthesize oldLocationCommModel = _oldLocationCommModel;
MAGICALLY SOLVES THE PROBLEM!
Regards
I just stumble upon your method declaration
-(void)loadView { ... }
In a view the first point you can rely that everything is fully initialized is after -(void)viewDidLoad was called. Maybe your code works on the simulator because your Mac is fast enough to cope this speed issue - but your mobile device isn't.
Maybe try this coding:
Your BaseViewController.h file:
@interface BaseViewController : UIViewController {
UIView *_vwHeader;
}
@end
Your BaseViewController.m file:
#import "BaseViewController.h"
@implementation BaseViewController
-(void)viewDidLoad {
[super viewDidLoad];
_vwHeader = [[UIView alloc] init];
}
Your CustomViewController.h file:
@interface CustomViewController : BaseViewController {
}
@end
Your CustomViewController.m file:
#import "CustomViewController.h"
-(void)viewDidLoad {
[super viewDidLoad];
[_vwHeader setHidden:NO];
}
Now your CustomViewController can rely on every instance variable in BaseViewController is correctly instantiated.
The error says that _vwHeader undeclared. So try by modifying the code in:
CustomViewController.m
#import "CustomViewController.h"
@implementation CustomViewController
- (void)loadView
{
[super loadView];
if(!_vwHeader)
{
_vwHeader = [[UIView alloc]init];
}
[_vwHeader setHidden:NO];
}
@end
It is possible that when you compile for target and for simulation the data members are either protected or private. Probably for target are private by default and this seems to cause the problem. Try out playing with the @private
and @protected
keywords.
However, I strongly suggest that you use properties even between your super/sub-classes. Having a complex structure is a bit hard to debug. Setting up a property will transfer the access to the data through the getter/setter methods (a breakpoint on @synthesize
also works) and you will be able to see in the call stack who is accessing what.
Especially, the syntax @synthesize propertyName = prefixDataNameSufix;
allows you to easily adjust your class interface style without having to modify your coding habits.
I had the exact same problem and it turns out that I did not remove an unused iVar/property in the SUBCLASS. Let's call it session
. I removed _session
from the iVar but I forgot to remove it from the properties, then in the .m file I had this synthesize session = _session
. Once I removed them all, I can compile for iOS device without problems.
If you think your superclass is fine, look into your subclass, check your iVars and properties and synthesize section in the .m file
I had this exact problem.
In my case, I was relying on ivar synthesis of a property. That is, I did NOT declare UITextView *textView_, but I did @synthesize textView = textView_;
This builds fine on my iOS Simulator. However, my iOS device build fails, regardless of whether I use llvm or gcc.
When I add the declaration back to my interface:
@interface MyTableViewController : BaseTableViewController {
@private
UITextView *textView_; // this line is important!
}
Everything works fine!
See the answer from tc above. This is a bug in the compiler shipped with sdk 4.2. Specifically I have seen the same error and if on the same machine I have sdk 4.2 and sdk 4.3 installed the error disappears (even if I compile for 4.2).
If anyone is having this issue after upgrading their tools and devices to iOS10, I had it and found that declaring them as weak, nonatomic in the .h file was the issue. I had never encountered this when doing so before but after removing (weak, nonatomic) from the property declaration, everything worked fine again.
精彩评论