我在xib中有uiButtons。我为所有人设置了恢复标识。我需要打印这些恢复ID列表。为此,我在viewDidload中调用以下代码:
-(void)loadViewFromNIB:(NSString *)nibName owner:(id)owner
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];
NSArray *subviews = [[objects objectAtIndex:0]subviews];
for (id key in subviews) {
[key addTarget:self
action:@selector(touchB:)
forControlEvents:UIControlEventTouchDown];
[key addTarget:self
action:@selector(touchE:)
forControlEvents:UIControlEventTouchUpInside];
NSString *ident = self.restorationIdentifier;
NSLog(@"%@",ident);
}
我得到了这个输出:
2013-02-24 13:05:38.817 fozbKEY[3939:11603] (null)
2013-02-24 13:05:38.822 fozbKEY[3939:11603] (null)
2013-02-24 13:05:38.824 fozbKEY[3939:11603] (null)
这只是重复了一堆。我做错了什么?我如何解决它?谢谢!
答案 0 :(得分:6)
您正在记录视图控制器的恢复ID。尝试记录按钮的恢复ID。现在你做:
NSString *ident = self.restorationIdentifier;
将该行更改为:
NSString *ident = [key restorationIdentifier];
对代码进行更好的更改将是:
for (UIView *subview in subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *key = (UIButton *)subview;
[key addTarget:self action:@selector(touchB:) forControlEvents:UIControlEventTouchDown];
[key addTarget:self action:@selector(touchE:) forControlEvents:UIControlEventTouchUpInside];
NSString *ident = key.restorationIdentifier;
NSLog(@"%@",ident);
}
}