[英]Load UIViewController inside a Container View using StoryBoard
我有一個UIViewController
稱為RootViewController
和A UIViewController
稱為NewsViewController
。 我想在UIView
顯示NewsViewController
( UIView
只是屏幕的一部分)我在RootViewController
創建。 我正在使用StoryBoard,我的應用程序需要支持iOS 5(因此我無法使用IB中的嵌入式segues和Containers)
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.
}
我還將兩個UIViewControllers與segue連接起來。 UIView newsSection將保持空白。 我究竟做錯了什么?
編輯:
這對我有用,是正確的方法嗎?
- (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];
}
迅速
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)
}
創建子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;
}
創建根視圖時添加子視圖:
-(void)viewDidLoad
{
[super viewDidLoad];
self.news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:self.news.view];
}
嘗試這個:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
NewsViewController *rootViewController =
[storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
這是它對我有用的方法。
我在頂部購買,銷售,租賃有3個按鈕。 現在在點擊任何特定按鈕我正在加載相應的UIViewController,我正在使用故事板。
我為每個UIViewController
指定了Storyboard Id
。
我在我的Main ViewController
添加了兩個UIView
,它們有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];
}
並且它對我的完美工作進一步你可以添加滾動視圖我想要在那里滾動。
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);
如果要執行動畫,請不要忘記指定約束
您應該將代碼移動到 - (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}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.