我有一个名为UIViewController
的{{1}}和一个名为RootViewController
的{{1}}。我想在UIViewController
内NewsViewController
内展示NewsViewController
(UIView
只是屏幕的一部分)。我正在使用StoryBoard,我的应用程序需要支持iOS 5(因此我无法使用IB中的嵌入式segues和容器)
RootViewController代码:
UIView
我还将两个UIViewControllers与segue连接起来。 UIView newsSection将保持空白。 我做错了什么?
修改
这对我有用,是正确的做法吗?
RootViewController
答案 0 :(得分:44)
- (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];
}
夫特
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:
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)
}
答案 1 :(得分:10)
创建子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;
}
创建root视图时添加子视图:
-(void)viewDidLoad
{
[super viewDidLoad];
self.news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:self.news.view];
}
答案 2 :(得分:1)
试试这个:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
NewsViewController *rootViewController =
[storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
答案 3 :(得分:1)
这是我的工作方式。
我在购买,销售,租赁的顶部有3个按钮。现在点击任何特定按钮我正在加载相应的UIViewController,我正在使用故事板。
我为每个Storyboard Id
指定了UIViewController
。
我向我的UIView
添加了两个Main ViewController
,其中有3个按钮(购买,销售,租赁)。
我用于上述三个按钮的一个视图以及我用于加载UIViewController的其他视图。
现在点击我的特定按钮并点击特定按钮我正在执行以下代码。
- (IBAction)sellingClicked:(id)sender
{
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
TestViewController *tvc=[storyboard instantiateViewControllerWithIdentifier:@"test"];
[self.viewContainer addSubview:tvc.view];
[self addChildViewController:tvc];
}
并且它更适合我,你可以添加滚动视图我想要滚动到那里。
答案 4 :(得分:0)
您应该将代码移动到 - (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.
}
因为viewDidLoad中的边框和边界是{0,0} {0,0}