如何访问对象恢复id(为什么它为null?)

foz*_*ios 3 null objective-c nslog nsarray ios

我在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);


}
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

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)
Run Code Online (Sandbox Code Playgroud)

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

rma*_*ddy 6

您正在记录视图控制器的恢复ID.尝试记录按钮的恢复ID.现在你做:

NSString *ident = self.restorationIdentifier;
Run Code Online (Sandbox Code Playgroud)

将该行更改为:

NSString *ident = [key restorationIdentifier];
Run Code Online (Sandbox Code Playgroud)

对代码进行更好的更改将是:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)