If a subclass is defined as MyView that subclasses UIView, and in the Interface Builder, the view is clicked on, and the Custom Class is already set to MyView.
But in ViewController’s viewDidLoad,
self.view.foo = ... ;
will create a compile error that says foo is not a property of UIView. I then print out the class of self.view:
NSLog(@"Class of view is %@", NSStringFromClass([self.view class]));
and sure enough, it prints out MyView. Also, not only the first line will not compile, Xcode will not “auto complete” the word “foo” as well. But clearly, the class prints out no as UIView but is MyView, and in my class definition of MyView, I did say:
@property (nonatomic, strong) NSMutableArray *foo;
in the interface .h file, and also in the implementation .m file:
@synthesize foo;
and MyView.h is imported in ViewController.h as well. Why is this so and how to fix this?
Your UIViewController doesn’t know that its view is meant to be an instance of your
MyViewsubclass.You’ll need to cast it
If you want to simplify this, you can also subclass UIViewController and then just always reference
self.myViewin your MyViewController subclasses instead ofself.view: