简体   繁体   中英

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!

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 !!!

使用ClassA的相同“ mainView”实例导航到ClassA View Controller

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

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM