3

I have uiButtons in an xib. I have set restoration ids for all of them. I need to print a list of these restoration ids. to do this i call the following code in 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);


}

i get this output:

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)

this just repeats a bunch. What I am doing wrong? how do I fix it? Thanks!

fozbstuios
  • 373
  • 4
  • 8
  • 17

1 Answers1

6

You are logging the view controller's restoration id. Try logging the button's restoration id. Right now you do:

NSString *ident = self.restorationIdentifier;

Change that line to this:

NSString *ident = [key restorationIdentifier];

An even better change to your code would be this:

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);
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • when I do this i get: Property 'restorationIdentifier' not found on object of type 'const __strong id' and clang: error: linker command failed with exit code 1 (use -v to see invocation) and (null): error: unable to open executable '/Users/fozbstudios/Library/Developer/Xcode/DerivedData/fozbKEY-fnhwwleozrshipawuzdrjnhcplgz/Build/Products/Debug-iphonesimulator/fozbKEY.app/fozbKEY' I think this is because key may be reserved by apple – fozbstuios Feb 24 '13 at 19:35
  • @fozbstuios I clarified my answer. – rmaddy Feb 24 '13 at 19:36
  • @rmaddyThat works. Is there any way to get rid of 2013-02-24 13:05:38.817 fozbKEY[3939:11603] so I just get the id? – fozbstuios Feb 24 '13 at 19:41
  • No. Calling NSLog always shows the timestamp and process that did the logging. – rmaddy Feb 24 '13 at 19:42