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

后端 未结 10 1283
时光取名叫无心
时光取名叫无心 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.

提交回复
热议问题