I'm diving into iOS development and I'm starting with a very simple example to understand using IB to create a custom view. I have a simple view controller...
@interface MyViewController ()
@property (weak, nonatomic) IBOutlet MyView *myView;
@end
@implementation MyViewController
//Boilerplate code...
And my custom view...
@implementation MyView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
and in the app delegate, I set the root view controller to MyViewController...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *myVC = [[MyViewController alloc] init];
self.window.rootViewController = myVC;
[self.window makeKeyAndVisible];
return YES;
}
So I have a nib for MyViewController
and a nib for MyView
. In the nib for MyViewController, I dragged a UIView into the center of it and set the class for that view to MyView
. When I run the app, I see the MyView
view with the red background color. Here's my question: when I comment out the line of code in the MyView
implementation file that sets the background color to red, and instead use the nib for MyView
to set the background color to some other color in interface builder, the background color of the view is white. Why can't I set the background color of MyView
in Interface Builder? Why does it only get set if I set it programmatically?
Thanks in advance for your wisdom!