我是否以编程方式在ViewDidAppear,ViewDidLoad,ViewWillAppear,构造函数中添加子视图? [英] Do I programmatically add SubViews in ViewDidAppear, ViewDidLoad, ViewWillAppear, the constructor?

查看:102
本文介绍了我是否以编程方式在ViewDidAppear,ViewDidLoad,ViewWillAppear,构造函数中添加子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Apple的粗略文档中找出哪种方法是初始化并将我的Views控件添加到控制器视图的最佳位置。

I'm trying to figure out from Apple's sketchy documentation which method is the best place to be initializing and adding my Views controls to the controller's view.

使用winforms它非常简单,因为它们总是在 InitializeDesigner中初始化,在构造函数中调用。我试图在可能的情况下匹配这种模式的可靠性。

With winforms it's fairly straightforward, as they're always initialized inside InitializeDesigner, called in the constructor. I'm trying to match the reliability of this pattern if possible.

我正在使用 UIViewControllers UITableViewControllers 大部分时间在 UINavigationController 内 - 如果这会影响所有。

I'm working with UIViewControllers and UITableViewControllers inside a UINavigationController most of the time - if this effects it all.

以下是一个例子:

public MyController()
{
    // Here?
    AddViews();
}

public override ViewDidLoad()
{
    base.ViewDidLoad();

    // Or is should it be here?
    AddViews();
}

public override ViewWillAppear(bool )
{
    base.ViewWillAppear(animated);

    // Here?
    AddViews();
}

public override ViewDidAppear(bool animated)
{
    base.ViewDidLoad(animated);

    // Or maybe here?
    AddViews();
}

void AddViews()
{
    UILabel label = new UILabel();
    label.Text = "Test";
    label.Frame = new RectangleF(100,100,100,26);
    View.AddSubView(label);

    UIWebView webview = new UIWebView();
    webview .Frame = new RectangleF(100,100,100,26);
    View.AddSubView(webview);
}

当我将它们添加到不同的视图中时,我得到了一些UIControl的混合结果地方。视觉滞后有时候,webview隐藏在某个地方。

I get mixed results with some UIControls when I add them to the view in different places. Visual lag sometimes, othertimes the webview is hidden somewhere.

是否有一般规则可以继续添加?

Is there a general rule to keep to for adding them?

推荐答案

一般来说,这就是我所做的:

In general, this is what I do:


  • ViewDidLoad - 每当我添加时控制到应该与视图一起出现的视图,我把它放在ViewDidLoad方法中。基本上,只要将视图加载到内存中,就会调用此方法。例如,如果我的视图是带有3个标签的表单,我会在这里添加标签;如果没有这些表单,视图将永远不会存在。

  • ViewDidLoad - Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.

ViewWillAppear :我通常只使用ViewWillAppear来更新表单上的数据。因此,对于上面的示例,我将使用它来实际将数据从我的域加载到表单中。创建UIViews是相当昂贵的,你应该尽可能避免在ViewWillAppear方法上这样做,因为当它被调用时,这意味着iPhone已经准备好向用户显示UIView,并且你在这里做的任何重量将以非常明显的方式影响性能(如动画延迟等)。

ViewWillAppear: I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).

ViewDidAppear :最后,我使用ViewDidAppear开始执行需要很长时间执行的事情的新线程,比如执行webservice调用以获取上面表单的额外数据。好处是因为视图已经存在并且正在向用户显示,在获取数据时,您可以向用户显示一条漂亮的等待消息。

ViewDidAppear: Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.

但是,您可以使用其他技巧。假设您希望UILabel在加载表单后飞到表单中。在这种情况下,我会将标签添加到ViewDidLoad中的表单,但在视图区域外面有一个Frame,然后在ViewDidAppear中我会动画将它拖回视图。

There are other tricks you can use, though. Lets say you want a UILabel to "fly" into the form after the form is loaded. In that case, I would add the label to the form in the ViewDidLoad but with a Frame outside of the view area, and then in the ViewDidAppear I would do the animation to fly it back into view.

希望有所帮助。

这篇关于我是否以编程方式在ViewDidAppear,ViewDidLoad,ViewWillAppear,构造函数中添加子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆