“Variable Undeclared” error when compiling to iOS Device, but not for Simulator

后端 未结 10 1285
时光取名叫无心
时光取名叫无心 2020-12-31 10:15

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.

相关标签:
10条回答
  • 2020-12-31 10:54

    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.

    0 讨论(0)
  • 2020-12-31 10:56

    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!

    0 讨论(0)
  • 2020-12-31 10:56

    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).

    0 讨论(0)
  • 2020-12-31 10:57

    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

    0 讨论(0)
  • 2020-12-31 10:57

    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
    
    0 讨论(0)
  • 2020-12-31 11:08

    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.

    0 讨论(0)
提交回复
热议问题