最初的问题:
我有一个从情节提要创建的 UITableViewController“tvc”作为静态单元格表。每个单元格包含一个 UILabel 和一个 UITextField。我希望遍历所有 UITextField 并弄乱它们的属性。我的解决方案是遍历 tvc.tableView.subviews 及其子视图等。使用下面的代码。
@implementation UIView (GWExtensions)
-(void) enumerateAllSubviewsWithBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop)) block {
[self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj,idx,stop);
if (*stop) {
return;
}
[(UIView*)obj enumerateAllSubviewsWithBlock:block];
}];
}
在 UITableViewController viewDidLoad 方法中:
[self.tableView enumerateAllSubviewsWithBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"\n\n=== SubView: %@", obj);
}];
我在日志中得到的是
=== SubView: <UIImageView: 0xa1ea760; frame = (0 476; 320 3); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0xa1ea840>> - (null)
=== SubView: <UIImageView: 0xa1ea980; frame = (316 390; 3 65); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0xa1eaa60>> - (null)
因此,由此我得出结论,我的 UITableViewController 包含 2 个 UIImageView。这显然是疯了。
一些反馈后的更多细节:
这是有趣的。在 [super viewDidLoad] 之后在 viewDidLoad 中运行 po [self.view recursiveDescription] 会产生以下结果:
<UITableView: 0xbadc400; frame = (0 0; 320 480); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0xa5dd570>; layer = <CALayer: 0xa5dcf20>; contentOffset: {0, 0}>
| <UIImageView: 0xa5dd120; frame = (0 476; 320 3); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0xa5dd200>> - (null)
| <UIImageView: 0xa5dd340; frame = (316 390; 3 65); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0xa5dd420>> - (null)
在 viewWillAppear 中运行 po [self.view recursiveDescription] 还会产生:
<UITableView: 0xbadc400; frame = (0 0; 320 480); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0xa5dd570>; layer = <CALayer: 0xa5dcf20>; contentOffset: {0, 0}>
| <UIImageView: 0xa5dd120; frame = (0 476; 320 3); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0xa5dd200>> - (null)
| <UIImageView: 0xa5dd340; frame = (316 390; 3 65); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0xa5dd420>> - (null)
但是,在 viewDidAppear 中运行 po [self.view recursiveDescription] 会得到有趣的结果:
<UITableView: 0xa8d3a00; frame = (0 64; 320 367); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0xa287c20>; layer = <CALayer: 0xa27de10>; contentOffset: {0, 0}>
| <UITableViewCell: 0x8bed650; frame = (0 275; 320 46); autoresize = W; layer = <CALayer: 0x8bed7c0>>
| | <UITableViewCellScrollView: 0x8bee2a0; frame = (0 0; 320 46); autoresize = W+H; gestureRecognizers = <NSArray: 0x8bee530>; layer = <CALayer: 0x8bee470>; contentOffset: {0, 0}>
| | | <UITableViewCellContentView: 0x8bed810; frame = (0 0; 287 44.5); gestureRecognizers = <NSArray: 0x8beee80>; layer = <CALayer: 0x8bed880>>
| | | | <UILabel: 0x8bed8d0; frame = (10 13; 76 20); text = 'Type'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8bed970>>
| | | | <UILabel: 0x8bedbf0; frame = (94 13; 216 20); text = 'Cadet'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8bedc90>>
| | | <UIButton: 0x8bee9b0; frame = (297 16; 8 12.5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8beea90>>
| | | | <UIImageView: 0x8beeb80; frame = (0 0; 8 12.5); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8beec10>> - (null)
| <UITableViewCell: 0x8beab60; frame = (0 231; 320 44); autoresize = W; layer = <CALayer: 0x8beacd0>>
| | <UITableViewCellScrollView: 0x8becdd0; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8bed040>; layer = <CALayer: 0x8becfa0>; contentOffset: {0, 0}>
| | | <UITableViewCellContentView: 0x8bead20; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x8bed610>; layer = <CALayer: 0x8bead90>>
| | | | <UILabel: 0x8beade0; frame = (10 11.5; 76 21); text = 'Years'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8beae80>>
| | | | <UITextField: 0x8beb130; frame = (94 7; 216 30); text = '0'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x8bebd90>; layer = <CALayer: 0x8beb270>>
| | | | | <_UIBaselineLayoutStrut: 0x8bec8f0; frame = (0 4.5; 0 21); clipsToBounds = YES; hidden = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8bec990>>
| | | | | <UITextFieldLabel: 0x8beb4b0; frame = (0 0; 216 30); text = '0'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8beb550>>
| <UITableViewCell: 0x8be82f0; frame = (0 187; 320 44); autoresize = W; layer = <CALayer: 0x8be8460>>
| | <UITableViewCellScrollView: 0x8bea2d0; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8bea540>; layer = <CALayer: 0x8bea4a0>; contentOffset: {0, 0}>
| | | <UITableViewCellContentView: 0x8be84b0; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x8beab20>; layer = <CALayer: 0x8be8520>>
| | | | <UILabel: 0x8be8570; frame = (10 11.5; 76 21); text = 'Grade'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8be8610>>
| | | | <UITextField: 0x8be8860; frame = (94 7; 216 30); text = '0'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x8be93b0>; layer = <CALayer: 0x8be89a0>>
| | | | | <_UIBaselineLayoutStrut: 0x8be9e00; frame = (0 4.5; 0 21); clipsToBounds = YES; hidden = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8be9ea0>>
| | | | | <UITextFieldLabel: 0x8be8b80; frame = (0 0; 216 30); text = '0'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8be8c20>>
| <UITableViewCell: 0x8be5c40; frame = (0 143; 320 44); autoresize = W; layer = <CALayer: 0x8be5db0>>
| | <UITableViewCellScrollView: 0x8be7b50; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8be7da0>; layer = <CALayer: 0x8be7d20>; contentOffset: {0, 0}>
| | | <UITableViewCellContentView: 0x8be5e00; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x8be82d0>; layer = <CALayer: 0x8be5e70>>
| | | | <UILabel: 0x8be5ec0; frame = (10 11.5; 76 21); text = 'Birthday'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8be5f60>>
| | | | <UITextField: 0x8be61b0; frame = (94 12; 216 20); text = 'Aug 6, 2140'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x8be6d00>; layer = <CALayer: 0x8be62f0>>
| | | | | <_UIBaselineLayoutStrut: 0x8be7720; frame = (0 -0.5; 0 21); clipsToBounds = YES; hidden = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8be77c0>>
| | | | | <UITextFieldLabel: 0x8be64d0; frame = (0 0; 216 20); text = 'Aug 6, 2140'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8be6570>>
| <UITableViewCell: 0x8be3380; frame = (0 99; 320 44); autoresize = W; layer = <CALayer: 0x8be34f0>>
| | <UITableViewCellScrollView: 0x8be5480; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8be56f0>; layer = <CALayer: 0x8be5650>; contentOffset: {0, 0}>
| | | <UITableViewCellContentView: 0x8be3520; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x8be5c20>; layer = <CALayer: 0x8be3590>>
| | | | <UILabel: 0x8be35c0; frame = (10 11.5; 76 21); text = 'Parent'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8be3660>>
| | | | <UITextField: 0x8be3870; frame = (94 7; 216 30); text = 'Sabrina'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x8be4440>; layer = <CALayer: 0x8be39b0>>
| | | | | <_UIBaselineLayoutStrut: 0x8be4f80; frame = (0 4.5; 0 21); clipsToBounds = YES; hidden = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8be5020>>
| | | | | <UITextFieldLabel: 0x8be3b90; frame = (0 0; 216 30); text = 'Sabrina'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8be3c30>>
| <UITableViewCell: 0x8bde3f0; frame = (0 55; 320 44); autoresize = W; layer = <CALayer: 0x8bc3e30>>
| | <UITableViewCellScrollView: 0x8be2a00; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8be2c70>; layer = <CALayer: 0x8be2bd0>; contentOffset: {0, 0}>
| | | <UITableViewCellContentView: 0x8bde300; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x8be3360>; layer = <CALayer: 0x8bcd980>>
| | | | <UILabel: 0x8bde560; frame = (10 11.5; 76 21); text = 'Name'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8bde370>>
| | | | <UITextField: 0x8bde760; frame = (94 7; 216 30); text = 'JoAnne Powell'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x8be19c0>; layer = <CALayer: 0x8bde8a0>>
| | | | | <_UIBaselineLayoutStrut: 0x8bde8d0; frame = (0 4.5; 0 21); clipsToBounds = YES; hidden = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8be12e0>>
| | | | | <UITextFieldLabel: 0x8bdf940; frame = (0 0; 216 30); text = 'JoAnne Powell'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8bdfa40>>
| <UIImageView: 0xa269c80; frame = (0 363; 320 3); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0xa272790>> - (null)
| <UITableViewHeaderFooterView: 0xf17bb60; frame = (0 0; 320 55); text = 'SCOUT'; autoresize = W; layer = <CALayer: 0xf17bc20>>
| | <_UITableViewHeaderFooterViewBackground: 0xf17bd40; frame = (0 0; 320 55); autoresize = W+H; layer = <CALayer: 0xf17bdb0>>
| | <_UITableViewHeaderFooterContentView: 0xf17bc50; frame = (0 0; 320 55); layer = <CALayer: 0xf17bcb0>>
| | | <_UITableViewHeaderFooterViewLabel: 0xf17be40; frame = (15 31.5; 49 17); text = 'SCOUT'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xf17bef0>>
| <UITableViewHeaderFooterView: 0xf17c440; frame = (0 331; 320 37.5); text = 'CONTACT'; autoresize = W; layer = <CALayer: 0xf17c500>>
| | <_UITableViewHeaderFooterViewBackground: 0xf17c630; frame = (0 0; 320 37.5); autoresize = W+H; layer = <CALayer: 0xf17c6a0>>
| | <_UITableViewHeaderFooterContentView: 0xf17c530; frame = (0 0; 320 37.5); layer = <CALayer: 0xf17c590>>
| | | <_UITableViewHeaderFooterViewLabel: 0xf17c730; frame = (15 14; 66 17); text = 'CONTACT'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xf17c7e0>>
| <UIImageView: 0xa284610; frame = (316 390; 3 65); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0xa2846a0>> - (null)
所以在视图出现之前我似乎无法操作 UITextFields。这没有意义。这个对吗?我怎样才能在它们出现之前摆弄它们?