[英]Load UIViewController inside a Container View using StoryBoard
I have a UIViewController
called RootViewController
and A UIViewController
called NewsViewController
. 我有一个UIViewController
称为RootViewController
和A UIViewController
称为NewsViewController
。 I want to show NewsViewController
inside a UIView
(the UIView
is just a part of the screen) I created inside RootViewController
. 我想在UIView
显示NewsViewController
( UIView
只是屏幕的一部分)我在RootViewController
创建。 I'm using StoryBoard and my app needs to support iOS 5 (so I can't use embedded segues aka Containers from the IB) 我正在使用StoryBoard,我的应用程序需要支持iOS 5(因此我无法使用IB中的嵌入式segues和Containers)
RootViewController code: RootViewController代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NewsViewController *news = [[NewsViewController alloc]init];
news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:news.view];
[self addChildViewController:news];
// Do any additional setup after loading the view.
}
I also connected both UIViewControllers with a segue. 我还将两个UIViewControllers与segue连接起来。 The UIView newsSection will stay empty. UIView newsSection将保持空白。 What Am I doing wrong? 我究竟做错了什么?
Edit: 编辑:
This works for me, is that the right approach? 这对我有用,是正确的方法吗?
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:news.view];
[self addChildViewController:news];
[news didMoveToParentViewController:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:news.view];
[self addChildViewController:news];
[news didMoveToParentViewController:self];
}
Swift 迅速
override func viewDidLoad() {
super.viewDidLoad()
let news = self.storyboard?.instantiateViewControllerWithIdentifier("NewsViewControllerID") as! NewsViewController
news.view.frame = newsSection.bounds
newsSection.addSubview(news.view)
addChildViewController(news)
news.didMoveToParentViewController(self)
}
Swift >= 3: Swift> = 3:
override func viewDidLoad() {
super.viewDidLoad()
let news = self.storyboard?.instantiateViewController(withIdentifier: "NewsViewControllerID") as! NewsViewController
news.view.frame = newsSection.bounds
newsSection.addSubview(news.view)
addChildViewController(news)
news.didMove(toParentViewController: self)
}
Create child, when creating root VC: 创建子VC时创建子VC:
-(void)awakeFromNib
{
[super awakeFromNib];
// Create news vc from storyboard
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"News VC ID, you should set it in IB"];
// Add it as a child to root
[self addChildViewController:news];
[news didMoveToParentViewController:self];
// Save it for future use
self.news = news;
}
Add child's view when root's view is created: 创建根视图时添加子视图:
-(void)viewDidLoad
{
[super viewDidLoad];
self.news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:self.news.view];
}
Try this: 尝试这个:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
NewsViewController *rootViewController =
[storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
Here is the approach the way it worked for me. 这是它对我有用的方法。
I have 3 buttons at the top Purchase,Sale, Rental. 我在顶部购买,销售,租赁有3个按钮。 now at tapping at any particular button i am loading corresponding UIViewController and i am using storyboard. 现在在点击任何特定按钮我正在加载相应的UIViewController,我正在使用故事板。
I specified Storyboard Id
for every UIViewController
. 我为每个UIViewController
指定了Storyboard Id
。
I added two UIView
to my Main ViewController
which have these 3 buttons(Purchase, Sale ,Rental). 我在我的Main ViewController
添加了两个UIView
,它们有3个按钮(购买,销售,租赁)。
one view i am using for the above three buttons and other i am using for loading a UIViewController. 一个视图我用于上面的三个按钮和其他我用于加载UIViewController。
Now at tapping a specific button i am and in tapping a specific button i am doing following code. 现在我点击一个特定的按钮,然后点击一个特定的按钮,我正在做下面的代码。
- (IBAction)sellingClicked:(id)sender
{
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
TestViewController *tvc=[storyboard instantiateViewControllerWithIdentifier:@"test"];
[self.viewContainer addSubview:tvc.view];
[self addChildViewController:tvc];
}
and its working perfectly for me further you can add scrolling View i you want to scroll there. 并且它对我的完美工作进一步你可以添加滚动视图我想要在那里滚动。
otherController!.view.frame = container.bounds;
self.addChildViewController(otherController!);
container.addSubview(otherController!.view);
container.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: .allZeros, metrics: nil, views: ["view": otherController!.view]))
container.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: .allZeros, metrics: nil, views: ["view": otherController!.view]))
otherController!.didMoveToParentViewController(self);
Don't forget to specify constraint especially if you want to perform animations 如果要执行动画,请不要忘记指定约束
You should move your code to - (void) viewDidAppear: (BOOL) animated like this 您应该将代码移动到 - (void)viewDidAppear:(BOOL)这样动画
- (void) viewDidAppear: (BOOL) animated
{
[super viewDidAppear: animated];
NewsViewController *news = [[NewsViewController alloc]init];
news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:news.view];
[self addChildViewController:news];
// Do any additional setup after loading the view.
}
because frame and bounds in viewDidLoad is {0,0} {0,0} 因为viewDidLoad中的边框和边界是{0,0} {0,0}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.