Jak*_*kub 2 objective-c storyboard uiviewcontroller ios
我有故事板:

正如你所看到我有MenuViewController:带容器的UIViewController.Container是 MenuTableViewController: 带有MenuTableView的UITableViewController:UITableView.
我想要做的是更改我的Container(MenuTableViewController)的大小,并将另一个Child添加到我的mainVC.
我在MenuViewController中添加它:
-(void)viewDidAppear:(BOOL)animated {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MenuTableViewController* secondChildVC = [sb instantiateViewControllerWithIdentifier:@"MenuTableViewController"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[self addChildViewController:secondChildVC];
NSLog(@"MenuTableViewController.view info: %@",secondChildVC.view);
NSLog(@"secondChildVC views: %@",[secondChildVC.view subviews]);
MenuTableViewController *firstChildVC = [self.childViewControllers objectAtIndex:0];
[firstChildVC.view setFrame:CGRectMake(160, 0, 160, 504)];
[firstChildVC.view setBackgroundColor:[UIColor greenColor]];
NSLog(@"firstChildVC views: %@",[firstChildVC.view subviews]);
}
Run Code Online (Sandbox Code Playgroud)
这是我的日志:
MenuTableViewController.view info: <MenuTableView: 0x8910e00; baseClass = UITableView; frame = (0 20; 320 548); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x761eca0>; layer = <CALayer: 0x76202b0>; contentOffset: {0, 0}>
2013-05-03 17:20:10.796 TestStories[41780:c07] secondChildVC views: (
"<UIImageView: 0x76205e0; frame = (0 541; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7620680>>",
"<UIImageView: 0x7620740; frame = (313 517; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x76207e0>>"
)
2013-05-03 17:20:10.798 TestStories[41780:c07] firstChildVC views: (
"<UITableViewCell: 0x7143110; frame = (0 320; 160 320); autoresize = W; layer = <CALayer: 0x713cc10>>",
"<UITableViewCell: 0x7140b90; frame = (0 0; 160 320); autoresize = W; layer = <CALayer: 0x7140ce0>>",
"<UIImageView: 0x7135000; frame = (0 497; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x71351b0>>",
"<UIImageView: 0x7135270; frame = (153 517; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7135310>>"
)
Run Code Online (Sandbox Code Playgroud)
为什么我的secondChildVC不包含TableViewCells?不知道.
您可以看到我使用红色和绿色作为背景(红色表示secondChildCV).但可能永远不会出现.这是截图:

rde*_*mar 12
我认为你做错了几件事.子视图控制器的大小与容器视图的大小相同,因此您应该有一个容器视图的IBOutlet,并更改其大小,而不是更改子视图控制器的大小.
对于第二个子项,您将其添加为子项,但是您永远不会将其视图添加到MenuViewController的视图中.您还应该在将其添加为子项后立即调用didMoveToParentViewController.所以你需要做类似的事情:
MenuTableViewController* secondChildVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MenuTableViewController"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[self addChildViewController:secondChildVC];
[secondChildVC didMoveToParentViewController:self];
secondChildVC.view.frame = CGRectMake(0,0,160,504);
[self.view addSubview:secondChildVC.view];
Run Code Online (Sandbox Code Playgroud)
另请注意,您可以使用self.storyboard更简单地获取对故事板的引用
| 归档时间: |
|
| 查看次数: |
14461 次 |
| 最近记录: |