3

我在 xib 中有 uiButtons。我已经为所有这些设置了恢复 ID。我需要打印这些恢复 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)

这只是重复一堆。我做错了什么?我如何解决它?谢谢!

4

1 回答 1

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);
    }
}
于 2013-02-24T19:25:18.000 回答