我是否以编程方式在 ViewDidAppear、ViewDidLoad、ViewWillAppear、构造函数中添加子视图?
我试图从苹果的 粗略 文档中找出哪种方法是最好的地方正在初始化并将我的视图控件添加到控制器的视图中。
对于 winforms,这相当简单,因为它们总是在 InitializeDesigner
内初始化,并在构造函数中调用。如果可能的话,我正在尝试匹配这种模式的可靠性。
我大部分时间都在 UINavigationController
内使用 UIViewControllers
和 UITableViewControllers
- 如果这会影响这一切的话。
下面是一个示例:
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 添加到不同位置的视图时,我得到了混合结果。有时视觉滞后,有时网络视图隐藏在某处。
添加它们是否有需要遵守的一般规则?
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.
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.
I'm working with UIViewControllers
and UITableViewControllers
inside a UINavigationController
most of the time - if this effects it all.
Here's an example:
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);
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,这就是我所做的:
ViewDidLoad - 每当我向应与视图一起出现的视图添加控件时,我都会立即将其放入 ViewDidLoad 方法中。基本上,只要视图加载到内存中,就会调用此方法。例如,如果我的视图是一个带有 3 个标签的表单,我会在此处添加标签;如果没有这些表单,视图将永远不会存在。
ViewWillAppear:我通常使用 ViewWillAppear 只是为了更新表单上的数据。因此,对于上面的示例,我将使用它来将数据从我的域实际加载到表单中。创建 UIView 的成本相当高,您应该尽可能避免在 ViewWillAppear 方法上执行此操作,因为当调用该方法时,这意味着 iPhone 已经准备好向用户显示 UIView,并且您在这里执行的任何繁重操作将以非常明显的方式影响性能(例如动画延迟等)。
ViewDidAppear:最后,我使用 ViewDidAppear 启动新线程来执行需要很长时间才能执行的操作,例如执行 Web 服务调用以获取上述表单的额外数据。好处是,由于视图已经存在并且正在向用户显示,因此您可以在获取数据时向用户显示一条漂亮的“等待”消息。
不过,您还可以使用其他技巧。假设您希望 UILabel 在加载表单后“飞”到表单中。在这种情况下,我会将标签添加到 ViewDidLoad 中的表单中,但框架位于视图区域之外,然后在 ViewDidAppear 中我将执行动画以将其飞回视图中。
希望有帮助。
In general, this is what I do:
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: 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: 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.
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.
Hope it helps.
嗯,苹果的文档似乎很清楚,恕我直言。
如果您以编程方式创建自己的根视图(此特定控制器视图层次结构的根视图),则应在
-loadView
中创建它,而不调用super
并设置完成后查看
属性。如果您的视图是从笔尖加载的,则不应触摸-loadView
。您可以将自定义子视图添加到视图控制器的视图中,或者在
-viewDidLoad
中对其进行修改。推荐的做法是在-viewDidLoad
中创建 UILabel 和 UIWebView 并在-viewDidUnload
中释放它们,如果需要,将它们的引用设置为nil
将它们保留在伊瓦尔中。注意:
-viewDidUnload
在 iOS 6 中已被弃用,并且不再被调用,因为UIViewController
不再在内存压力下清除其视图。Hmm, Apple's docs seem to be pretty clear, IMHO.
If you create your own root view (the root view of this particular controller's view hierarchy) programmatically, you should create it in
-loadView
without callingsuper
and set theview
property when done. If your view is loaded from a nib, you should not touch-loadView
.You add custom subviews to the view controller's view or otherwise modify it in
-viewDidLoad
. The recommended practice is to create your UILabel and UIWebView in-viewDidLoad
and release them in-viewDidUnload
, setting their references tonil
if you need to keep them in ivars.Note:
-viewDidUnload
is deprecated in iOS 6 and is just not called any more, becauseUIViewController
no longer purges its view under memory pressure.viewDidLoad 与“MEMORY”相关,viewWillAppear/viewDidAppear 与“APPEARANCE”相关。视图控制器的视图(这是视图控制器视图的根视图)可以多次出现/消失,即使控制器的视图已经在内存中。
(当提到根视图时,我也指它的子视图,因为根视图引用了它的子视图(子视图),但从视图控制器的角度来看,它通常只知道根视图。对子视图的引用通常可以发生通过视图控制器的出口。)
当出现内存警告时,根视图本身可能会从内存中删除。视图控制器将确定何时是从内存中删除它们的最佳时间。
因此,您通常会在 viewDidLoad 中添加子视图,因为添加子视图意味着将它们添加到内存中。但如果您以编程方式创建所有视图(不是从 nib 文件),则不会。如果是这种情况,那么您应该重写 loadView 方法,并创建一个根视图并在其中添加子视图,因此在这种情况下您可以省略 viewDidLoad 来添加子视图。
viewDidLoad relates to 'MEMORY', and viewWillAppear/viewDidAppear relates to 'APPEARANCE'. A view controller's view (which is a root-view of your view controller's views) can appear/disappear numerous times, even if the controller's view is already in memory.
(When referring to root-view, I also means its subviews, because the root-view has reference to its children (subviews), but from a view controller's perspective, it usually knows just the root-view. Reference to subviews can happens normally through view controller's outlets.)
The root-view itself MAY be removed from memory when there's a memory warning. The view controller will determine when is the best time to removed them from memory.
So, you would normally add subviews in viewDidLoad, because adding subviews means adding them to memory. BUT not if you create all of your views programmatically (not from a nib files). If that's the case then you should override loadView method, and create a root-view and add subviews there, so in this case you can omit viewDidLoad to add subviews.