0

I have 2 views which are part of UITabBarController. For each view I declared a different class.

PictureViewController with the method:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [imagePicker dismissModalViewControllerAnimated:YES];
    [imageField setImage:image];
}

And another view: AdjustViewController with another UIImage:

@property (weak, nonatomic) IBOutlet UIImageView *viewImage;

I would like in the above method - didFinishPickingImage to set the value of viewImage in AdjustViewController to the selected image.

How can I do it?

Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

1

EDIT: TO set your image in AppDelegate , u will have to create property for UIImage *image in AppDelegate and assign the image like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{ 
   MyAppDelegateClass *appDelegate = (MyAppDelegateClass  *)[[UIApplication sharedApplicaton] delegate];
  appDelegate.image=image; //your picked image here

  [imageField setImage:image];
  [imagePicker dismissModalViewControllerAnimated:YES];

}

The quick and dirty way to pass data is to add attributes to the app delegate and then call the app delegate from the view controllers using:

MyAppDelegateClass *appDelegate= (MyAppDelegateClass  *)[[UIApplication sharedApplicaton] delegate];
viewImage.image=appDelegate.image;

The best place to retrieve changing data is in the viewWillAppear controller method. That way the data will be updated every time the user switches to that tab.


You might want to consider NSNotificationCenter (Reference); you register the one viewcontroller with the application notification center, and send a notification when a selection is made. When the notification is received, the other viewcontroller updates itself accordingly

Refer more on this link

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • do you mean: 1. add to AppDelegate.h file a property image 2. Add both lines to viewWillApear controller method and how do I assign the image value to the AppDelegate in didFinishPicking method? – Dejell Aug 29 '12 at 19:00
1

Since both of these are in the tabBarController, you can use the tabBarController to get a reference to the other view controller and access its properties from there.

Like so:

NSArray *theViewControllers = [self.tabBarController viewControllers];

//On this line, you will need to use the Index of the AdjustViewController (0 is on the left and then they go in order from left to right.)
AdjustViewController *adjViewController = (AdjustViewController *)[theViewControllers objectAtIndex:0];

adjViewController.viewImage.image = image;

That will assign the image to the viewImage property of the AdjustViewController that is on the UITabBarController assuming you use the right index.

OR, if you like to compress things into as few lines as possible:

((AdjustViewController *)[[self.tabBarController viewControllers] objectAtIndex:0]).viewImage.image = image;
Justin Paulson
  • 4,388
  • 1
  • 23
  • 28
  • In the above method, like you asked for. – Justin Paulson Aug 29 '12 at 20:07
  • Thanks for the answer, however, when the user clicks on the adjust button in the bottom bar, the image is not set. Any idea why? Any function maybe in AdjustViewController that can initialize it? – Dejell Aug 30 '12 at 06:17
  • Did you use the above code and change the index to the proper index of the AdjustViewController? Does the AdjustViewController do anything with the image on viewWillAppear? Was the AdjustViewController loaded already? – Justin Paulson Aug 30 '12 at 13:40
  • Yes. The adjust is number 1 (if I leave number 0 - I get an exception that the variable does not appear there). There is no method in AdjustViewController.m called viewWillAppear. How can I know if it was loaded already? – Dejell Aug 30 '12 at 15:50
  • Most likely it is not loaded unless you have tapped that tab already. You could set up the viewDidLoad method in `AdjustViewController` to pull the image from the `PictureViewController` using the same method except use the index of the `PictureViewController`. – Justin Paulson Aug 30 '12 at 16:02
  • viewDidLoad or viewWillApear? – Dejell Aug 30 '12 at 16:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16049/discussion-between-odelya-and-justin-paulson) – Dejell Aug 30 '12 at 16:31