views:

46

answers:

3

I need help with a problem I cant seem to solve.

I have a viewcontroller(view1) which I show by presenting it.

This view(view1) has a button on it, which when selected, presents another viewController(view 2). View 2 has a few buttons, when one is selected it opens a new viewController(view3).

Now the thing is, when a button is selected in view 3, I want to create a UIImageView which holds the image which was selected(the button which was pressed in view3) in view1 and display it.

How do I create a UIImageView with an assigned image in view1, when the action is triggered in view3?

I currently create an image but nothing is set to or shows up on view1. I wont even show the code as it is a big fail.

Thanks Thanks

A: 

You asked the same question here and you already got an answer. Posting the same question multiple times is not going to help you.

In essence: keep a reference to view1 in view3 and use some helper methods in the first view controller.

muffix
It does help. I tried your method and it doesnt work. Asking a question and getting an answer "In essence" doesnt help. I know I need a reference and helper methods, but I dont know how to do it. Thats why I am asking.Twice. :)
alJaree
A: 

You can try with below sample code:


// TestAppDelegate.h

#import <UIKit/UIKit.h>
@interface TestAppDelegate : NSObject{
    UIImage *image;
}
@property (nonatomic, retain) UIImage *image;
@end

// TestAppDelegate.m

#import "TestAppDelegate.h"
@implementation TestAppDelegate
@synthesize image;
------
------
------
@end

// TestViewController.m    // sample code

#import "TestViewController.h"
#import "TestAppDelegate.h"

@implementation TestViewController

-(void)setImage{          // If you want to set an image, you can use this method. if you use like this, you can get this image from any view controller.
    TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
    appDelegate.image = self.image;
}

-(void)getImage{          // If you want to get image, you can this method.
    TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
    self.image = appDelegate.image;

}

@end

If you set a particular image in TestAppDelegate, you can access that image from any view controller using "-(void)getImage" method.

jfalexvijay
Thanks a lot. I will try something similar. What about having multiple images, As each button that is chosen, should create a UIImage and set an image to it.
alJaree
+1  A: 

From my previous answer, just replace UIImage with NSMutableDictionary in "TestAppDelegate"


UIImage *img1 = [UIImage imageNamed:@"image1.png"];
UIImage *img2 = [UIImage imageNamed:@"image2.png"];
UIImage *img3 = [UIImage imageNamed:@"image3.png"];

[imageDictionary setObject:img1 forKey:@"button1"];
[imageDictionary setObject:img2 forKey:@"button2"];
[imageDictionary setObject:img3 forKey:@"button3"];

if you want to get the particular image, just use below sample code.


TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
self.image = [appDelegate.imageDictionary valueForKey:@"button2"];
jfalexvijay