不知道这个是不是搜索“add UIViewController in subview”的右键。 正如您在我的图像中看到的那样,有两个 ViewController,主 Controller 和第二个 Controller 。在主 Controller 内部有一个 UIView(蓝色背景色)。在 UIView 中,我想在我的 UIView 中添加第二个 ViewController。我有这段代码,但没有用。
这是我的代码
#import "ViewController.h"
#import "SampleViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *testView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SampleViewController * sample = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);
[self.testView addSubview:sample.view];
}
@end
我想知道这是否可行?我知道 initWithNibName:
在 xib 文件中工作,我不知道在谷歌中搜索这个的确切术语。如果这在 IOS 中可行,我只是想尝试一些东西。希望你明白我想做什么。希望得到您的建议。提前致谢
这是我的更新
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *testView;
@property(strong,nonatomic) SampleViewController * samples;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIStoryboard *storyBoard = self.storyboard;
SampleViewController * sample = [storyBoard instantiateViewControllerWithIdentifier:@"SampleViewController"];
// SampleViewController * sample = [[SampleViewController alloc] //initWithNibName:@"SampleViewController" bundle:nil];
[self displayContentController:sample];
//commented the below line because it is not needed here, use it when you want to remove
//child view from parent.
//[self hideContentController:sample];
}
- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.bounds = self.testView.bounds; //2
[self.testView addSubview:content.view];
[content didMoveToParentViewController:self]; // 3
}
- (void) hideContentController: (UIViewController*) content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[content removeFromParentViewController]; // 3
}
我总是得到这个错误
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/ace/Library/Developer/CoreSimulator/Devices/035D6DD6-B6A5-4213-9FCA-ECE06ED837EC/data/Containers/Bundle/Application/EB07DD14-A6FF-4CF5-A369-45D6DBD7C0ED/Addsubviewcontroller.app> (loaded)' with name 'SampleViewController''
我认为,它正在寻找 Nib 。我没有在这里实现 Nib 。
最佳答案
您应该使用子包含概念,这里 MainViewController 是父 View Controller ,您想要将 subview Controller View 添加为主视图 Controller 上的 subview 。
添加和删除子项
//call displayContentController to add SampleViewCOntroller view to mainViewcontroller
[self displayContentController:sampleVCObject];
// write this method in MainViewController
- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.bounds = testView.bounds; //2
[testView addSubview:content.view];
[content didMoveToParentViewController:self]; // 3
}
代码的作用如下:
它调用容器的 addChildViewController: 方法来添加 child 。调用 addChildViewController: 方法也会自动调用 child 的 willMoveToParentViewController: 方法。 它访问 subview 的属性以检索 View 并将其添加到自己的 View 层次结构中。容器在添加 View 之前设置 child 的大小和位置;容器总是选择子内容出现的位置。尽管此示例通过显式设置框架来实现,但您也可以使用布局约束来确定 View 的位置。 它显式调用子项的 didMoveToParentViewController: 方法来表示操作已完成。
//you can also write this method in MainViewController to remove the child VC you added before.
- (void) hideContentController: (UIViewController*) content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[content removeFromParentViewController]; // 3
}
在 Interface Builder 中配置容器,适合那些不想编写代码的人。
要在设计时创建父子容器关系,请将容器 View 对象添加到 Storyboard场景中,如图 5-3 所示。容器 View 对象是一个占位符对象,表示 subview Controller 的内容。使用该 View 相对于容器中的其他 View 调整 subview 的大小和位置。
当您使用一个或多个容器 View 加载 View Controller 时,Interface Builder 还会加载与这些 View 关联的 subview Controller 。子项必须与父项同时实例化,以便可以创建适当的父子关系。
关于ios - 在 subview 中添加 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614776/