Monotouch:从视图中删除所有子视图
我正在尝试从 UIView 中删除所有子视图。我尝试了以下方法没有效果:
for (int i = 0; i < this.Subviews.Length; i++)
{
this.Subviews[i].RemoveFromSuperview ();
}
I am trying to remove all the subviews from a UIView. I tried the following to no effect:
for (int i = 0; i < this.Subviews.Length; i++)
{
this.Subviews[i].RemoveFromSuperview ();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
刚刚测试了这个,它对我有用。 (虽然你的代码对我来说也看起来不错......)
如果它不适合你,可能有一些东西阻止子视图被删除。
Just tested this and it worked for me. (Although your code also looks good to me...)
If it doesn't work for you, there might be something that prevents the subviews from being removed.
您的示例的问题在于您如何构建循环。
当您删除 0 处的视图时,子视图数组会缩短一个元素,并且元素 1 在下一次迭代时将变为元素 0。另一方面,你的 i 变量不断增长,所以你最终会跳过视图 1。
The problem with your sample is how you built the loop.
When you remove the view at 0, the Subviews array is one element shorter, and element 1 becomes element 0 on the next iteration. Your i variable on the other hand keeps growing, so you end up skipping view 1.
之后尝试强制刷新视图,或专门在主线程上调用Remove 调用。
Try forcing a refresh of the view afterward, or invoking the Remove call specifically on the main thread.
如果你绝对需要使用 for 循环,这就可以了
If you absolutely need to use an for loop, this will do