I'm new to iOS and Xcode. I can't figure out how to design a separated view and make it be added into the main UIViewController using storyboard.
I did different approaches..
UIViewController
. In the main ViewController.m, I get the new UIViewController
instance in the viewDidLoad, and then [self.view addSubview:newUIViewController.view]
. But I can't see the added view.ViewController
. And addSubview
with the xib's view. But it also failed.Is there a correct way or any working solution to do so?
I figured out a way to do it. Described as following:
The key-point is to override the initWithCoder method in the object-c class.
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
The idea is the custom class is loaded by the storyboard and initWithCode will be called. The index 0 is the root view in the .xib interface builder.
It's kind of tricky but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With