0
votes

I am building an iPhone Utility app that uses UIImageView to display an animation. Within the MainViewController's viewDidLoad() method, I am creating an instance of a CustomClass, and then setting up the animations:

- (void)viewDidLoad 
{
 [super viewDidLoad]; 
 cc = [CustomClass new];

 NSArray * imageArray = [[NSArray alloc] initWithObjects:
          [UIImage imageNamed:@"image-1-off.jpg"],
          [UIImage imageNamed:@"image-2-off.jpg"],
          [UIImage imageNamed:@"image-3-off.jpg"],
          [UIImage imageNamed:@"image-4-off.jpg"],
          nil];
 offSequence = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
 offSequence.animationImages = imageArray;
 offSequence.animationDuration = .8;
 offSequence.contentMode = UIViewContentModeBottomLeft;
 [self.view addSubview:offSequence];
 [offSequence startAnimating];
}

That works fine. However, I would like to be able to move all the above code that sets up the UIImageView into my CustomClass. The problem is in the second to last line:

[self.view addSubview:offSequence];

I basically need to replace 'self' with a reference to the MainControllerView, so I can call addSubview from within my CustomClass. I tried creating an instance var of CustomClass called mvc and a setter method that takes a reference to the MainViewController as an argument as such:

- (void) setMainViewController: (MainViewController *) the_mvc 
{
 mvc = the_mvc;
}

And then I called it within MainViewController like so:

[cc setMainController:MainViewController:self];

But this yields all sorts of errors which I can post here, but it strikes me that I may be overcomplicating this. Is there an easier way to reference the MainViewController that instanatiated my CustomClass?

1

1 Answers

0
votes

The cleanest way to do this would be to create a subclass of UIImageView and create a customize initializer that accepts the array. So,

@interface MyCustomImageView:UIImageView {
//...
}

-(id) initWithFrame(CGRect) aRect animationImages:(NSArray *) imageArray;
@end

Then in your main view controller just initialize the custom image view, populate it and add it to the subviews. This will make everything nicely encapsulated, modular and reusable.