【发布时间】:2015-02-21 06:22:45
【问题描述】:
我不知道这是否是搜索“在子视图中添加 UIViewController”的正确键。 正如您在我的图像中看到的那样,有两个 ViewController,主控制器和第二控制器。在主控制器内部有一个 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''
我想,它正在寻找一个笔尖。我这里没有实现笔尖。
【问题讨论】:
-
通过这个objc.io/issue-12/…学习
-
@user3818576 我根据您的需要编辑了我的答案,请使用它并告诉我,如果我能解决更多问题。
-
@user3818576 1. 您正在使用 nib 文件或情节提要?
-
故事板。我没有 nib 文件
-
@user3818576 我终于编辑了你的代码,请使用它并告诉我。
标签: ios objective-c uiview uiviewcontroller