Steven French
Steven French

Reputation: 7

Objective-C: Change an image from a separate class

I have looked around for an answer to this with no avail, so I hope I am not asking an answered question.

I have two classes (ClassA and ClassB), both connected to UIViewControllers that are linked via a segue. ClassA contains a UIImage named backgroundImage. My goal is to change ClassA's background image from within ClassB.

In ClassB.h I have tried:

@property (nonatomic, retain) ClassA *mainView;

And in ClassB.m I tried changing the background image via:

@synthesize mainView;
//Then inside button click I tried:
[mainView.backgroundImage setImage:[UIImage imageNamed:@"newImage.jpg"]];

Obviously this did not work since I am not setting the image to the same ClassA that is being used in the app. I know I'm missing something obvious, thanks in advance!

Upvotes: 0

Views: 450

Answers (5)

Gaurav Rastogi
Gaurav Rastogi

Reputation: 2145

You can do this task easily using Custom Delegates or Notifications . I will explain it to you using Notification . Let there be two classes - Class-A & Class-B

In the Class-A add a Notification like this

 [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(changeImage)
                                             name:@"ImageChangeNotification"
                                           object:nil];

also create the selector in the Class-A

 -(void)changeImage
   {
       [yourImageView setImage:[UIImage imageNamed:@"YouImageName.png"]];
   }

What left is just to post this Notification from anywhere within the whole project . In our case let it be Class-B

  [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageChangeNotification" object:nil];

Upvotes: 0

iDhaval
iDhaval

Reputation: 7844

Please use the Protocol mechanism to satisfy your requirements

Implementation is as bellow

In AppDelegate.h file of project

@protocol ImageChangeDelegate <NSObject>
@optional
    -(void)ChangeImage;
@end


@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
}
@property (nonatomic,strong) id<ImageChangeDelegate> delegateImageChange;

In AppDelegate.m file of project

@synthesize delegateImageChange;

Now In .h file of Class B

#import "AppDelegate.h"

@interface EventGuestListViewController : UIViewController
{
   AppDelegate* appDelegate;
}
-(void)ChangeBackgroundOfClassA;

Now In .m file of Class B

- (void)viewDidLoad
{
    [super viewDidLoad];
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

Now in method of changing BGs

 -(void)ChangeBackgroundOfClassA
 {
     [appDelegate.delegateImageChange ChangeImage];
 }

Now Implements the Protocol in Class A to change the Background.

Now In .h file of Class A

 #import "AppDelegate.h"

 @interface EventGuestListViewController : UIViewController <ImageChangeDelegate>
 {
    IBOutlet UIImageView* imageBGClassA;
    AppDelegate* appDelegate;
 }

Now In .m file of Class A

- (void)viewDidLoad
{
    [super viewDidLoad];
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.delegateImageChange = self; //Here you need to set the delegate to 'self' to call then Custom Protocol method
}

-(void)ChangeImage
{
     imageBGClassA.image = [UIImage imageNamed:@"newImage.jpg"];
}

In Class B once -(void)ChangeBackgroundOfClassA call the delegate method call which is implement on Class A and the background Image will surly change

This is work with me

It may help you !!!

Upvotes: 1

Harjot Singh
Harjot Singh

Reputation: 6927

You can try any of two ways: 1) Using the delegation. Passing the delegate while loading ClassB. 2) Using the NotificationCenter and change the mainView Image by calling the notification from Class B.

Harry.

Upvotes: 0

Lithu T.V
Lithu T.V

Reputation: 20021

Should try out delegate mechanism.It will allow you to call a method in classA from classB. In that method change the background image of classA from classB's call to delegate

Upvotes: 0

NSUserDefault
NSUserDefault

Reputation: 1834

Use the same "mainView" instance of ClassA to navigate to ClassA View Controller.

Upvotes: 0

Related Questions