gpt4 book ai didi

ios - 使用 StoryBoard 在容器 View 中加载 UIViewController

转载 作者:IT王子 更新时间:2023-10-29 07:53:46 25 4
gpt4 key购买 nike

我有一个名为 RootViewControllerUIViewController 和一个名为 NewsViewControllerUIViewController。我想在 UIView 中显示 NewsViewController(UIView 只是屏幕的一部分)我在 RootViewController 中创建>。我正在使用 StoryBoard 并且我的应用程序需要支持 iOS 5(因此我不能使用来自 IB 的嵌入式 segues aka 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.
}

我还用 segue 连接了两个 UIViewControllers。 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];
}

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:

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)
}

关于ios - 使用 StoryBoard 在容器 View 中加载 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com