Pregunta

i'm getting "use of undeclared identifier" errors in my .m file with the code below and can't seem to work it out.

NSArray *imageViews = [NSArray arrayWithObjects:img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18, img19, img20, img21, img22, img23, img24, img25, img26, img27, img28, img29, img30, img31, img32, img33, img34, img35, img36, img37, img38, img39, img40, nil];

In my .h file i have 40 images, all with referencing outlets:

@property (weak, nonatomic) IBOutlet UIImageView *imgX;

where X is a number from 1-40. In my .m file, the NSArray *imagesViews works fine as long as it's inside a method, but i can't declare it outside the method so that it is available to all methods. As an Objective-C novice, I don't where to go from here. I'd appreciate any help.

¿Fue útil?

Solución

You don't have to initialize the array outside of a method to make it accessible from all methods. What you should do instead is declare it as a property and initialize it inside the viewDidLoad method.

In the .h file:

@property (strong, nonatomic) NSArray *imageViews;
@property (weak, nonatomic) IBOutlet UIImageView *img1;
// ...

In the .m file:

@synthesize imageViews, img1, img2, ...
// ...

- (void)viewDidLoad
{
    [super viewDidLoad];
    // ...
    self.imageViews = [NSArray arrayWithObjects:self.img1, self.img2, ... , nil];
}

Also, note that because you have 40 image views, you should probably avoid declaring a property for each one of them. You can assign tags to them, and then retrieve them using the method viewWithTag.

Otros consejos

In the header:

@interface MyClass : NSObject {
    NSArray *imageViews;
}

@end

In the implementation:

@implementation MyClass

- (id) init
{
    self = [super init];
    if (self != nil) {
        imageViews = [[NSArray arrayWithObjects:img1, nil] retain];
    }
    return self;
}

// now you can use imageViews also from other methods

- (void) dealloc
{
    [imageViews release];
    [super dealloc];
}

@end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top