本文介绍了为什么self.view.subviews在viewDidLoad中是一个空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Interface Builder创建了一堆UI元素,并使用IBOutlets将它们连接到我的ViewController。

I've created a bunch of UI elements using Interface Builder and hooked them up to my ViewController using IBOutlets.

然而,当我尝试迭代 self.view.subviews 在我的ViewController的 viewDidLoad 方法中,我发现子视图数组为空。

However, when I try to iterate through self.view.subviews in my ViewController's viewDidLoad method, I find that the subviews array is empty.


  • ViewController.xib

UIView
|
- UILabel
- UIButton
- // ... more stuff
- UIToolbar


  • ViewController.m

    #import "ViewController.h"
    @interface ViewController ()
    // Interface elements
    @property IBOutlet UILabel *titleLabel;
    @property IBOutlet UIButton *button1;
    // ... etc
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // BREAK_POINT
    
        // ... code to wire up the UIButtons to dynamically created objects
    }
    


  • 调试器输出 BREAK_POINT

    (lldb) po self.view
    <UIView: 0x7fa7897e2140; frame = (0 0; 600 600); autoresize = W+H; layer = <CALayer: 0x7fa7897e2210>>
    
    (lldb) po self.view.subviews
    <__NSArrayI 0x7fa789713500>(
    
    )
    (lldb) po [self.view.subviews count];
     nil
    (lldb) po self.button1
    <UIButton: 0x7fa78955ea70; frame = (-23 -15; 46 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fa7897663d0>>
    


  • 为什么self.views.subviews是的任何想法当UIButtons等已经初始化并正确连接到IBOutlets时为空?

    Any ideas why self.views.subviews is empty when clearly the UIButtons, etc have been initialized and wired up to the IBOutlets correctly?

    编辑1:我重命名为 MainView.xibtoViewController.xib并删除了我的 ViewController.m 中的 loadView 实现,而我'我仍然看到相同的 self.view.subviews 行为。

    Edit 1: I renamed "MainView.xib" to "ViewController.xib" and removed the loadView implementation in my ViewController.m, and I'm still seeing the same self.view.subviews behavior.

    推荐答案

    尝试这样做

    - (void)viewDidLayoutSubviews{
    
    
    }
    

    加载所有子视图后调用此方法。希望这会对你有所帮助:)

    This method get called after loading all the subviews. Hope this will help you :)

    这篇关于为什么self.view.subviews在viewDidLoad中是一个空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-31 05:01