1

在 IOS 5.1 上,我在故事板上的视图控制器中添加了一个容器视图。我想通过 连接该容器视图IBOutlet,以便在 IB 上轻松管理它。

我尝试使用常规UIView

@property (nonatomic,weak) IBOutlet UIView *leftView; //connected this to container view on interface builder

那么,如果我使用它,它就不起作用

[self.leftView addSubview:tableVC.view];

如果我以编程方式添加一个 childview 控制器,它可以工作,但它当然会覆盖全屏。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                  bundle:nil];
    UITableViewController *tableVC = [sb instantiateViewControllerWithIdentifier:@"LogMasterViewController"];
    [self.view addSubview:tableVC.view];

    [self addChildViewController:tableVC];
    [tableVC didMoveToParentViewController:self];
}

如何将容器视图连接到子视图控制器?

4

1 回答 1

1

我刚刚测试了以下,它工作正常:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                  bundle:nil];
    UITableViewController *tableVC = [sb instantiateViewControllerWithIdentifier:@"LogMasterViewController"];
    [self.leftView addSubview:tableVC.view];
    tableVC.view.frame = self.leftView.bounds;

    [self addChildViewController:tableVC];
    [tableVC didMoveToParentViewController:self];
}

几个想法:

  1. 如果您LogMasterViewController不是 a UITableViewController,而是带有 a 的视图控制器UITableView,您可能需要确保该 tableview 上没有自动布局或自动调整大小的约束,从而使其将 tableview 的大小调整为您无法再看到的东西。

  2. 您可能想要运行该应用程序,在应用程序完成显示屏幕后暂停它(我在其中放置了一个断点viewDidAppear......该点之后的任何内容也应该可以工作,但不要执行viewDidLoador viewWillAppear),LogMasterViewController然后self.leftView输入在调试器控制台中执行以下命令:

    po [[UIWindow keyWindow] recursiveDescription]
    

    您可能希望提供唯一标签编号leftView的主要视图,LogMasterViewController以便您可以更轻松地破译输出。无论如何,使用它recursiveDescription来确定表格视图是否已呈现,但只是以一种您看不到表格的方式,或者根本没有添加它。您还可以确认您的leftView(带有唯一tag编号,如果您给它的话)也在那里。

    (lldb) po [[UIWindow keyWindow] recursiveDescription]
    $0 = 0x071a7a60 <UIWindow: 0x758e7a0; frame = (0 0; 320 480); autoresize = W+H; layer = <UIWindowLayer: 0x75ab340>>
       | <UIView: 0x71a8480; frame = (0 20; 320 460); autoresize = RM+BM; tag = 1; layer = <CALayer: 0x71a84e0>>
       |    | <UIView: 0x71a8ce0; frame = (0 0; 160 460); autoresize = TM+BM; tag = 2; layer = <CALayer: 0x71a8510>>
       |    |    | <UITableView: 0x7a9d400; frame = (0 0; 160 460); clipsToBounds = YES; opaque = NO; autoresize = W+H; tag = 3; gestureRecognizers = <NSArray: 0x71aba30>; layer = <CALayer: 0x71ab100>; contentOffset: {0, 0}>
       |    |    |    | <UITableViewCell: 0x71bbd50; frame = (0 396; 160 44); text = 'Row 10'; autoresize = W; layer = <CALayer: 0x71bbe80>>
       |    |    |    |    | <UITableViewCellContentView: 0x71bbeb0; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71bc100>; layer = <CALayer: 0x71bbf10>>
       |    |    |    |    |    | <UILabel: 0x71bc1e0; frame = (10 0; 140 43); text = 'Row 10'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71bbd20>>
       |    |    |    |    | <UIView: 0x71bc5c0; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71bb560>>
       |    |    |    | <UITableViewCell: 0x71bb5c0; frame = (0 352; 160 44); text = 'Row 9'; autoresize = W; layer = <CALayer: 0x71bb6f0>>
       |    |    |    |    | <UITableViewCellContentView: 0x71bb720; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71bb970>; layer = <CALayer: 0x71bb780>>
       |    |    |    |    |    | <UILabel: 0x71bba50; frame = (10 0; 140 43); text = 'Row 9'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71bb590>>
       |    |    |    |    | <UIView: 0x71bc560; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71bae50>>
       |    |    |    | <UITableViewCell: 0x71baeb0; frame = (0 308; 160 44); text = 'Row 8'; autoresize = W; layer = <CALayer: 0x71bafe0>>
       |    |    |    |    | <UITableViewCellContentView: 0x71bb010; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71bb260>; layer = <CALayer: 0x71bb070>>
       |    |    |    |    |    | <UILabel: 0x71bb340; frame = (10 0; 140 43); text = 'Row 8'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71bae80>>
       |    |    |    |    | <UIView: 0x71bc500; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71ba740>>
       |    |    |    | <UITableViewCell: 0x71ba7a0; frame = (0 264; 160 44); text = 'Row 7'; autoresize = W; layer = <CALayer: 0x71ba8d0>>
       |    |    |    |    | <UITableViewCellContentView: 0x71ba900; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71bab50>; layer = <CALayer: 0x71ba960>>
       |    |    |    |    |    | <UILabel: 0x71bac30; frame = (10 0; 140 43); text = 'Row 7'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71ba770>>
       |    |    |    |    | <UIView: 0x71bc4a0; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71ba030>>
       |    |    |    | <UITableViewCell: 0x71ba090; frame = (0 220; 160 44); text = 'Row 6'; autoresize = W; layer = <CALayer: 0x71ba1c0>>
       |    |    |    |    | <UITableViewCellContentView: 0x71ba1f0; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71ba440>; layer = <CALayer: 0x71ba250>>
       |    |    |    |    |    | <UILabel: 0x71ba520; frame = (10 0; 140 43); text = 'Row 6'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71ba060>>
       |    |    |    |    | <UIView: 0x71b8a70; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71b9910>>
       |    |    |    | <UITableViewCell: 0x71b9970; frame = (0 176; 160 44); text = 'Row 5'; autoresize = W; layer = <CALayer: 0x71b9aa0>>
       |    |    |    |    | <UITableViewCellContentView: 0x71b9ad0; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71b9d20>; layer = <CALayer: 0x71b9b30>>
       |    |    |    |    |    | <UILabel: 0x71b9e00; frame = (10 0; 140 43); text = 'Row 5'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71b9940>>
       |    |    |    |    | <UIView: 0x71b8010; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71b9200>>
       |    |    |    | <UITableViewCell: 0x71b9260; frame = (0 132; 160 44); text = 'Row 4'; autoresize = W; layer = <CALayer: 0x71b9390>>
       |    |    |    |    | <UITableViewCellContentView: 0x71b93c0; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71b9610>; layer = <CALayer: 0x71b9420>>
       |    |    |    |    |    | <UILabel: 0x71b96f0; frame = (10 0; 140 43); text = 'Row 4'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71b9230>>
       |    |    |    |    | <UIView: 0x71bb4f0; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71b8af0>>
       |    |    |    | <UITableViewCell: 0x71b8b50; frame = (0 88; 160 44); text = 'Row 3'; autoresize = W; layer = <CALayer: 0x71b8c80>>
       |    |    |    |    | <UITableViewCellContentView: 0x71b8cb0; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71b8f00>; layer = <CALayer: 0x71b8d10>>
       |    |    |    |    |    | <UILabel: 0x71b8fe0; frame = (10 0; 140 43); text = 'Row 3'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71b8b20>>
       |    |    |    |    | <UIView: 0x71bade0; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71b8090>>
       |    |    |    | <UITableViewCell: 0x71b80f0; frame = (0 44; 160 44); text = 'Row 2'; autoresize = W; layer = <CALayer: 0x71b8220>>
       |    |    |    |    | <UITableViewCellContentView: 0x71b8250; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71b85a0>; layer = <CALayer: 0x71b82b0>>
       |    |    |    |    |    | <UILabel: 0x71b8790; frame = (10 0; 140 43); text = 'Row 2'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71b8630>>
       |    |    |    |    | <UIView: 0x71ba6d0; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71b9fe0>>
       |    |    |    | <UITableViewCell: 0x71b4d30; frame = (0 0; 160 44); text = 'Row 1'; autoresize = W; layer = <CALayer: 0x71b4e80>>
       |    |    |    |    | <UITableViewCellContentView: 0x71b4fb0; frame = (0 0; 160 43); gestureRecognizers = <NSArray: 0x71b5b80>; layer = <CALayer: 0x71b5040>>
       |    |    |    |    |    | <UILabel: 0x71b65d0; frame = (10 0; 140 43); text = 'Row 1'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x71b66e0>>
       |    |    |    |    | <UIView: 0x71b98a0; frame = (0 43; 160 1); autoresize = W+TM; layer = <CALayer: 0x71bbca0>>
       |    |    |    | <UIImageView: 0x71ac5d0; frame = (0 453; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x71ac700>>
       |    |    |    | <_UITableViewSeparatorView: 0x71b3a90; frame = (0 483; 160 1); opaque = NO; autoresize = W; layer = <CALayer: 0x71b3b10>>
       |    |    |    | <_UITableViewSeparatorView: 0x71b3dd0; frame = (0 527; 160 1); opaque = NO; autoresize = W; layer = <CALayer: 0x71b3e30>>
       |    |    |    | <_UITableViewSeparatorView: 0x71b3e60; frame = (0 571; 160 1); opaque = NO; autoresize = W; layer = <CALayer: 0x71b3ec0>>
       |    |    |    | <_UITableViewSeparatorView: 0x71b3ef0; frame = (0 615; 160 1); opaque = NO; autoresize = W; layer = <CALayer: 0x71b3f50>>
       |    |    |    | <_UITableViewSeparatorView: 0x71b3f80; frame = (0 659; 160 1); opaque = NO; autoresize = W; layer = <CALayer: 0x71b3fe0>>
       |    |    |    | <_UITableViewSeparatorView: 0x71b4030; frame = (0 703; 160 1); opaque = NO; autoresize = W; layer = <CALayer: 0x71b4090>>
       |    |    |    | <UIImageView: 0x71ac810; frame = (153 541; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x71ac8b0>>
    

    在这个例子中,我的根控制器的主视图 atag为“1”,leftViewatag为“2”,主视图的主视图LogMasterViewControllertag“3”。在这种情况下,这些显示为我输出的第二、第三和第四行(我还看到了UITableViewCells下面的列表)。

于 2013-02-05T18:23:45.467 回答