ios - ios/objective c:迭代 UIView.subviews 仅显示部分视图
问题描述
我从这里学到了如何在 UIView 上迭代目标 c / 记录视图层次结构,但这个问题太老了,无法评论:
// UIView+HierarchyLogging.h
@interface UIView (ViewHierarchyLogging)
- (void)logViewHierarchy;
@end
// UIView+HierarchyLogging.m
@implementation UIView (ViewHierarchyLogging)
- (void)logViewHierarchy
{
NSLog(@"%@", self);
for (UIView *subview in self.subviews)
{
[subview logViewHierarchy];
}
}
@end
// In your implementation
[myView logViewHierarchy];
我的问题:在调试模式下,当且仅当我在 for 循环中设置断点并在 XCode 中单击“快速查看”时,一切正常。之后,所有子视图都被正确记录。在普通运行中,仅记录几个子视图。我究竟做错了什么?
提前感谢乔
解决方案
我认为链接的答案在理论上是正确的,但是当你真正想要理解输出时它有点不切实际。在输出中,您看不到任何层次结构,因为您看不到哪个视图是哪个视图的子视图。您至少应该使层次结构有点可见。例如:
some-view
another-view
whatever
foo
hello you
看起来不太好。当它看起来像这样时会更好:
(V:) some-view
(V:) another-view
(V:) whatever
(V:) foo
(V:) hello you
在第二个片段中,您看到“another-view”和“hello you”都是“some-view”的子视图。您还可以看到 logViewHierarchy 只被调用了一次。在第一个(假设的)调试输出中,无法知道 logViewHierarchy 被调用的频率。通过输出中的“(V:)”,您可以知道哪些调试输出行来自对 logViewHierarchy 的调用,哪些调试输出行来自其他地方!NSLog(@"%@", obj);
用;记录一个对象几乎从来都不是一个好主意。开头应该总是有一条日志消息,以便您可以在源代码中搜索它。(大多数互联网教程NSLog
的使用方式完全不适合生产环境,恕我直言。)
// UIView+HierarchyLogging.m
@implementation UIView (ViewHierarchyLogging)
- (void)_logViewHierarchyWithIndent:(NSString *)indentation
{
NSLog(@"%@(V:) %@", indentation, self);
NSString *more_indentation = [indentation stringByAppendingString:@" "];
for(UIView *v in self.subviews) {
[v _logViewHierarchyWithIndent: more_indentation];
}
}
- (void)logViewHierarchy
{
[self _logViewHierarchyWithIndent:@""];
}
@end
推荐阅读
- vbscript - 在qtp中给出相对路径
- java - 为什么 JavaFX TextField 侦听器不起作用?
- python - 如何检索 tf.image.decode_jpeg 返回的图像张量和高度?
- mysql - 有没有办法在 ALTER TABLE 期间关闭临时表的创建?
- vba - 第一行有问题,无法解决
- java - I want to add marker from arrayList on google map in the interval of 2 seconds
- c# - TFS API - 如何知道 TestRun 是否与 PBI 或 Bug 相关
- mysql - 从mysql获取今天和昨天的数据
- php - 如何安全连接以获得安全 cookie
- mysql - 如何将 IndexedDB 与 MySQL 同步上传特定表?