-1

I'm programming an application for road safety, and there will be a lots of code, so what I thought is I will divide each function into a class.

Now I'm trying to set the background of the main ViewController From another class, however it doesn't work. This is the code that I tried:

-(void)StartBackgroundAnimation
{
ViewController *MainRoot = [[ViewController alloc] init];
MainRoot.BackgroundViewer.image = [UIImage imageNamed:@"Lunch_Background.png"];
NSLog(@"ok");
 }

What I did is that I created a function called StartBackgroundAnimation, and I imported the main view-controller class, and I created an object from it. After that, I imported this class to the main view-controller, and I called this function but the image doesn't work.

Note: NSLogworks perfectly and the function is called however the image doesn't change.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
SniperCoder
  • 823
  • 2
  • 11
  • 23
  • 2
    It makes absolutely no sense to create a class for each function, you will have thousands of classes – Dante Puglisi Aug 11 '15 at 18:16
  • i know but this is the main question :) – SniperCoder Aug 11 '15 at 18:17
  • 2
    When you do `ViewController *MainRoot = [[ViewController alloc] init];` you are creating another instance of that controller, it is not the one you are viewing – Dante Puglisi Aug 11 '15 at 18:18
  • so how i can change the image from another class what i have to do? – SniperCoder Aug 11 '15 at 18:24
  • It's not that easy, you can create a function inside the `ViewController` you want to change and then, call that function from the second `ViewController` – Dante Puglisi Aug 11 '15 at 18:27
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers). In this case, the data to pass is a reference to another view controller. – Marcus Adams Aug 11 '15 at 18:55

2 Answers2

0

If you really want to store functions in different classes just return a UIImage and assign it in the view controller.

So inside your custom class.

- (UIImage*)backgroundImage{
 return [UIImage imageNamed:@"Lunch_Background.png"];
}

Inside of your main view controller:

self.view.backgroundView.image = [instanceofyourclass backgroundImage];
mKane
  • 932
  • 13
  • 30
0

If you'd like to access data from a class outside of a class, it is usually best in Objective-C to use a property.

// MyClass.h
@interface MyClass : Superclass
@property (strong, nonatomic) UIImage * backgroundImage;
@end

This way, you can read/write the property by using self.backgroundImage from within the class and anotherClass.backgroundImage from outside of it.

Edit: spelling...

iamthearm
  • 513
  • 2
  • 15
  • Hmm.. Are you actually setting the image property of the `UIImageView` to be `backgroundImage`? you can put in a function in your .m that will run whenever the `backgroundImage` property is changed. `-(void)setBackgroundImage:(UIImage *)image{myImageView.image = image; _backgroundImage = image;}`. Both lines are important. the first sets the on-screen view's image to `image` and the second changes the stored value. You will need to update the first line with a reference to your image view. – iamthearm Aug 11 '15 at 18:50