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

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

问题描述

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

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.

我大部分时间都在 UINavigationController 中使用 UIViewControllersUITableViewControllers - 如果这会影响到它.

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 来启动新线程来处理需要很长时间执行的事情,例如进行网络服务调用以获取额外数据好在因为视图已经存在并且正在显示给用户,所以您可以在获取数据的同时向用户显示一个很好的等待"消息.

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天全站免登陆