0

I have a the first application controller, MAViewControllerMenu, and when that controller loads, I already allocate the next controller, imageControllerView.

- (void)viewDidAppear{
    [super viewDidAppear:(YES)];
    if (!imageControllerView)
        imageControllerView = [[self storyboard] instantiateViewControllerWithIdentifier:@"chosenImageController"];
}

Then, I select an image from the image picker, and want to move to the next controller,imageControllerView, where the image would be displayed. I set the next window's image property as follows:

imageControllerView.image = [[self.pageViews objectAtIndex:(centered_image_ind)] image];

This line works, I checked that there's a value in imageControllerView.image. However, when I move to the next controller,imageControllerView , I notice that the memory address of imageControllerView changes, or in other words, imageControllerView's properties that I change before moving to that controller, specifically image, reset when I move there.

Instead of throwing code here, I was hoping you could let me know what I should provide. I think it's a common problem people know of: Controller's objects re-init'ing when moving from one controller to another.

Here's a screen shot that might give a hint of what Im trying to do

Left most one is where I select pictures which in turn go into the slide show scrollview. Then I click next, and the image is supposed to appear in the centered ImageView

Thanks

enter image description here

Larme
  • 24,190
  • 6
  • 51
  • 81
Alon_T
  • 1,430
  • 4
  • 26
  • 47
  • Warning sirens have been set off by that storyboard. Are you using the "curvy" segues to "pop" back from one view controller to the previous view controller? – Fogmeister Mar 25 '14 at 17:07
  • Just putting an answer together. – Fogmeister Mar 25 '14 at 17:10
  • I use modals. Also how can you tell I'm using them wrong? – Alon_T Mar 25 '14 at 17:10
  • There is an awful lot of arrows in that storyboard! Look at a tutorial, e.g. [tutorial](http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/) – Groot Mar 25 '14 at 17:10
  • @Alon_T just answering now. Will let you know. – Fogmeister Mar 25 '14 at 17:11
  • @Alon_T delete the backwards curly segues. They will break your app! – Fogmeister Mar 25 '14 at 17:28
  • Also, that UINavigationController will never be created. It looks like you're using it to create the navigation bars with the back buttons but they won't actually be there in the running app because the nav controller won't exist. – Fogmeister Mar 25 '14 at 17:30
  • @Alon_T remember to up vote and accept answers that solve your problem. – Fogmeister Mar 25 '14 at 17:55

2 Answers2

0

OK...

You cannot "already allocate the next view controller" this won't work. There is no point in creating it like this at all. You can delete the imageViewController property (or iVar) completely.

The arrows that you have between the view controllers in your storyboard are segues. In Interface Builder you can select a segue and give it an identifier. For instance you would use something like @"ImageViewSegue".

I guess the segue is attached to the Next button. This is fine.

Now, in your MAViewControllerMenu you need to put this method...

- (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ImageViewSegue"]) {
        // the controller is CREATED by the segue.
        // the one you create in view did load is never used
        ImageViewController *controller = segue.destinationController;
        controller.image = [[self.pageViews objectAtIndex:(centered_image_ind)] image];
    }
}

Now for the segues in the other direction...

You appear to be using segues to dismiss the modal views. You can't do this. What it will do is create a new view controller and present that instead of dismissing the presented view.

i.e. you'll go...

A -> B -> C -> B -> A -> B
// you'll now have 6 view controllers in memory
// each segue will create a fresh view controller with no values set.

What you want is...

A -> B -> C
A -> B
A
// now you only have one view controller because the others were dismissed.
// when you dismiss a view controller it will go back to the original one.
// the original one will have all the values you set previously.

To do this you need to create a method something like...

- (IBAction)dismissView
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

Then whatever the button is for your dismiss action attach it to this method.

Now delete all the backwards curly segues.

Passing info back

To pass info back to the original view controller you need a delegation pattern or something similar.

You can read more about creating a delegate at This random Google Search

Create a delegate method something like...

- (void)imageViewSelectedImage:(UIImage *)image;

or something like this.

Now when you do prepareForSegue you can do...

controller.delegate = self;

and have a method...

- (void)imageViewSelectedImage:(UIImage *)image
{
    // save the method that has been sent back into an array or something
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Hi, thanks for the great comment. I do however have to mention that I want to be able to go back to the previous controller with the slideshow without losing the memory there - that is, the picture the user had already selected. – Alon_T Mar 25 '14 at 17:19
  • I'm uploading a new picture as I got rid of some segues. – Alon_T Mar 25 '14 at 17:19
  • @Alon_T If you want to pass stuff backwards then you need to use a `delegation` pattern or something similar. I'll update briefly. – Fogmeister Mar 25 '14 at 17:20
  • i think that http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 gives a great example which is similar to yours..only saw it now on similar problems – Alon_T Mar 25 '14 at 17:24
  • Yeah, there are several different ways of getting data back from a view controller. However, mine is equally valid and all of them will still do stuff inside `prepareForSegue` :) – Fogmeister Mar 25 '14 at 17:27
-1

I might be wrong, but seems you go to your second view controller using a segue, it is normal your controller instance isn't the same than the one retrieved by [[self storyboard] instantiateViewControllerWithIdentifier:@"chosenImageController"]

you should take a look at - (void) prepareForSegue:(UIStoryboardSegue *)segue

(UIViewController method)

inside this method set your image property to the segue destination controller (check the identifier of the segue)

Jerome Diaz
  • 1,746
  • 8
  • 15
  • I do use a segue. I will check it out. Is there a better way to move from one controller to another without having a controller losing its values? For the record, my source controller loses it's values too – Alon_T Mar 25 '14 at 17:13
  • @Alon_T RE your source controller losing values. Please see my backwards segues explanation. This is why. You are creating fresh view controllers each time and moving to the new one instead of going backwards to the original one. – Fogmeister Mar 25 '14 at 17:18