use*_*576 15 objective-c uiviewcontroller uiview ios
我不知道这是否是搜索"在子视图中添加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
Run Code Online (Sandbox Code Playgroud)
我想知道这是否可行?我知道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
}
Run Code Online (Sandbox Code Playgroud)
我总是得到这个错误
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''
Run Code Online (Sandbox Code Playgroud)
我想,它正在寻找一个笔尖.我没有在这里实现一个笔尖.
San*_*uja 46
你应该使用Child Contament概念,这里MainViewController是一个父视图控制器,你想在子视图控制器上添加子视图控制器视图作为子视图.
添加和删除子项
//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
}
Run Code Online (Sandbox Code Playgroud)
这是代码的作用:
它调用容器的addChildViewController:方法来添加子进程.调用addChildViewController:方法也会自动调用子进程的willMoveToParentViewController:方法.它访问子视图属性以检索视图并将其添加到其自己的视图层次结构中.容器在添加视图之前设置子项的大小和位置; 容器总是选择孩子的内容出现的位置.虽然此示例通过显式设置框架来执行此操作,但您也可以使用布局约束来确定视图的位置.它显式调用子的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
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅apple doc:https: //developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
在Interface Builder中为那些不想编写代码的人配置容器.
要在设计时创建父子容器关系,请在故事板场景中添加容器视图对象,如图5-3所示.容器视图对象是占位符对象,表示子视图控制器的内容.使用该视图可以根据容器中的其他视图调整子视图的根视图的大小和位置.
使用一个或多个容器视图加载视图控制器时,Interface Builder还会加载与这些视图关联的子视图控制器.必须在父项的同时实例化子项,以便可以创建适当的父子关系.
Rav*_*avi 13
您只需使用StoryBoards即可完成此操作
小智 6
我已经在第二个uiviewcontroller上解决了关于uiview的问题.按照我使用的代码:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *sb = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:@"sb"];
sb.view.backgroundColor = [UIColor redColor];
[sb willMoveToParentViewController:self];
[self.view addSubview:sb.view];
[self addChildViewController:sb];
[sb didMoveToParentViewController:self];
Run Code Online (Sandbox Code Playgroud)
我希望能帮助你.
非常感谢
| 归档时间: |
|
| 查看次数: |
31250 次 |
| 最近记录: |