简体   繁体   English

没有使用我的自定义UIViewController调用ViewDidLoad

[英]ViewDidLoad not being called with my custom UIViewController

I have created a custom UIViewController class that creates a ScrollView at runtime that it loads into the view. 我创建了一个自定义的UIViewController类,它在运行时创建ScrollView,并将其加载到视图中。 See code here in the constructor of my custom UIViewController. 请参阅我的自定义UIViewController的构造函数中的代码。

        initControl(id, canEdit);

        _controllers = new NSMutableArray(0); //required to keep view controllers around

        _scrollView = new UIScrollView();
        _scrollView.BackgroundColor = UIColor.Green;
        this.View = _scrollView; 

ViewDidAppear and ViewWillAppear are called normally. ViewDidAppear和ViewWillAppear被正常调用。

ViewDidLoad is not called which I am not sure why as the view is showing up on the screen just fine. ViewDidLoad没有被调用,我不知道为什么因为视图显示在屏幕上就好了。

Any ideas? 有任何想法吗?

The viewDidLoad method is being called when accessing self.view 访问self.view时正在调用viewDidLoad方法

Examples: 例子:

1) 1)

- (id) init {
      self = [super init];
      if (self)
      {
          ...
          [self.view addSubview: self.toolbar];
      }
 }

2) 2)

viewContrl = [[MyViewController alloc] init];
viewContrl.view = webTopView;

3) 3)

viewContrl = [[MyViewController alloc] init];
[viewContrl.view addSubview: webTopView];

ViewDidLoad is called when you are allocating the view. 分配视图时会调用ViewDidLoad。 So if you are allocating the view once & only adding every time using addSubview then it called first time only. 因此,如果您要分配视图一次并且每次仅使用addSubview添加,那么它仅首次调用。 If you want to called it every time when you are adding it, then you needs to allocate it every time. 如果你想在每次添加它时调用它,那么你需要每次都分配它。 Also handle memory management by releasing the view before allocating it, if it is already allocated. 如果已经分配了视图,还可以在分配视图之前释放视图来处理内存管理。 Another way is to create a method which contains the operations which you wants to perform & called it after addSubview. 另一种方法是创建一个方法,其中包含您要执行的操作并在addSubview之后调用它。 It may solves your problem, if you have any doubt then feel free to ask me. 它可以解决您的问题,如果您有任何疑问,请随时问我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM