How do I reference my MainViewController from another class?
        Posted  
        
            by todd412
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by todd412
        
        
        
        Published on 2010-06-12T16:08:25Z
        Indexed on 
            2010/06/12
            16:12 UTC
        
        
        Read the original article
        Hit count: 324
        
Hi, 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?
© Stack Overflow or respective owner