Reputation: 5109
This is a ridiculously stupid question, but I'm having trouble with it. I've created one .xib with three UIViews in it. One Green View, one Red View, and one Blue View. The Green View displays when I run in simulator. I have put 2 buttons on my Green View. One to show the Red View and one to show the Blue View. Here is my code for the Green View:
#import "IDGreenView.h"
#import "IDRedView.h"
#import "IDBlueView.h"
#import "IDGreenView.h"
@interface IDGreenView()
@property (weak, nonatomic) IBOutlet UIButton *RedViewButton;
@property (weak, nonatomic) IBOutlet UIButton *BlueViewButton;
@end
@implementation IDGreenView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (IBAction)showRedView:(id)sender {
[self.window addSubview:[[IDRedView alloc]init]];
}
- (IBAction)showBlueView:(id)sender {
[self.window addSubview:[[IDBlueView alloc]init]];
}
@end
Here is my code for the Red View:
#import "IDRedView.h"
@implementation IDRedView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end
I can see it going into the Red View's initWithFrame method, so I know the call is OK. What do I need to do to make the Red View display?
Thank you.
-------------------------------------------------- Updated Comment --------------------------------------------------
Nope. I put the following code in the ViewController and that didn't work either.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.view addSubview:[[IDRedView alloc] initWithFrame:self.view.bounds]];
}
Why will it not display?
Upvotes: 0
Views: 1093
Reputation: 16865
From quick glance, your red view is not "Red" as you might assume.
You say you created the 3 views in ONE xib, but I see that they are defined in 3 files. The red view you get by calling [IDRedView alloc] init] is NOT the one you created in the xib. Thus, it isn't "red".
You really should have 3 xibs for the 3 views if you want to allocate them like that, but then you will need to use initWithNibName: to allocate them.
To see that your adding the views correctly and that your not crazy:
Try this, in red view -
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundcolor = [UIColor redColor];
}
return self;
}
Upvotes: 1
Reputation: 15566
Instead of self.window
try just using self
. But in this situation you are not really replacing the view you are creating a new one and placing it inside the current one. So if you kept going with this eventually you would end up with dozens of nested views.
Also you will want to use the initWithFrame:
init. So you would end up with:
- (IBAction)showRedView:(id)sender {
[self addSubview:[[IDRedView alloc] initWithFrame:self.bounds]];
}
The above implementation would completely hide your current view and just show the new one.
Upvotes: 1
Reputation: 6432
I see two main problems with your code:
First, why do you add the view to the window? [self.window addSubview...]
.
If IDGreenView
is a subclass of UIView
you should only say: [self addSubview...]
Second, you're creating IDRedView
and IDBlueView
without a frame (again, assuming they're both subclasses of UIView
). Try creating them with a frame, like so:
// You may have to adjust the frame to fit your needs
[[IDRedView alloc] initWithFrame:CGRectMake(0,0,20,20)];
Hope this helps!
Upvotes: 2