0

I have a ViewController which has an embedded UIImageView. Another ViewController has a button which should change the UIImageView's .image by click.

What is the best way to achieve this?

I've read something about a singleton class but:

  • I'm not quite sure if I get the point of a singleton class correctly, maybe so. could explain it to me in easy words?
  • There is a way using the AppDelegate ... but I think that's not the best solution.

Thank you.

UPDATE:

Ich want to call the setUpExternalDisplay-method (which uses instance variables) in FirstAssistantViewController. My Code:

FirstAssistantViewController.h

#import <UIKit/UIKit.h>
#import "DetailTableViewController.h"

@interface FirstAssistantViewController : UIViewController <DetailTableViewControllerDelegate>
{
DetailTableViewController * detailTableViewController;
}

DetailTableViewController.h

#import "AssistantRootViewController.h"
#import "FirstAssistantViewController.h"

@protocol DetailTableViewControllerDelegate
- (void) setUpExternalDisplay;
@end

@interface DetailTableViewController : UITableViewController <UISplitViewControllerDelegate, UIActionSheetDelegate, DetailTableViewControllerDelegate>
{
id <DetailTableViewControllerDelegate> delegate;
}
- (void)showModalHelpViewController;
- (void)showModalAssistantViewController;
@end

UPDATE 2:

It now does the following error messages:

Cannot find protocol declaration for 'DetailTableViewControllerDelegate'; did you mean 'UIPageViewControllerDelegate'?

Property 'DTVCdelegate' requires method 'DTVCdelegate' to be defined - use @synthesize, @dynamic or provide a method implementation

If I @synthesize:

Existing ivar 'DTVCdelegate' for unsafe_unretained property 'DTVCdelegate' must be __unsafe_unretained

Don't know how to solve it.

DAS
  • 1,941
  • 2
  • 27
  • 38

4 Answers4

0

Could that button in the other view controller be a Bool (on/off) so when it changes (true>false) the image also changes? Just an idea...Ill think about it

Szwedo
  • 354
  • 1
  • 3
  • 15
0

First off, get yourself familiarized with how to use protocols and call methods between 2 different view controllers from this answer How to use delegate methods to pass objects between two view controllers?.

Let object1(ViewController1) have the imageView and object2(ViewController2) have the buttons. All of the buttons are connected to a single method. I assume you have the images in object1. So, what you need is use some kind of way to identify the image in object1, perhaps the button's tag. Now you should have an object delegate in object2 which points to object1. You can then call in the button method

[self.delegate changeImageTo:button.tag]//I am just using tag in this case, but you can use other ways

In your ViewController1, you should implement the changeImageTo:

-(void) changeImageTo:(NSUInteger)imageID
{
imageView.image=//get it from the image source using imageID
[imageView setNeedsDisplay];
}

Create a protocol in ViewContoller2 as ViewController2Delegate

@protocol ViewController2Delegate
-(void) changeImageTo:(NSUInteger)imageID;
@end

and create delegate as an instance variable in ViewController2 class.

id <ViewController2Delegate> delegate;

Don't forget to create a property for it too.

@property (nonatomic,assign) id <ViewController2Delegate> delegate;

and synthesize in the .m file

@synthesize delegate;

And implement ViewController2Delegate protocol in ViewController1 class.

Community
  • 1
  • 1
MadhavanRP
  • 2,832
  • 1
  • 20
  • 26
  • @Darwin I ve updated my answer. When you create object 2 just assign its delegate to object1 – MadhavanRP Dec 29 '11 at 12:00
  • I does not work. I think your response is really good but I do not understand how to code this. I did the `@protocol` and the `id – DAS Dec 29 '11 at 12:08
  • @Darwin Did you implement the changeImageTo: method in VC1.m ? Is your delegate nil or did you not assign object2.delegate=object1 when creating it? – MadhavanRP Dec 29 '11 at 12:15
  • @Darwin Very much welcome. You had forgotten to create the property. It should work now. – MadhavanRP Dec 29 '11 at 12:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6231/discussion-between-madhavanrp-and-darwin) – MadhavanRP Dec 29 '11 at 12:35
  • It says `Cannot find protocol declaration`? And the second error is something like (at the @synthesize row) `delegate must be __unsafe_unretained`? – DAS Dec 29 '11 at 12:37
0

take one string in secondviewcontroller and declare the property and synthesize it.Then when you are going from firstviewcontroller to second view then change the string like this second view.somestring= @"one.png".In view did load of secondviewcontroller use the same string to change the image.

Tendulkar
  • 5,550
  • 2
  • 27
  • 53
0

You can also try to use NSNotification.

Akki
  • 1,487
  • 14
  • 25