0

I feel ignorant for asking this because I know it's simple. Goal: Retain a variable on a view after leave then returning to it.

For instance: Let's say we have MainView, CategoryView and (drumroll) ProjectView

Application opens to MainView it displays a table - user selects they want to pick a category. This segue's them to CategoryView. Once a selection is made I send the chosen category back to MainView. via

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

No problem here. MainView receives variable and displays the chosen category as subtext in the 'Category' section. Next the user wants to pick a project name (this is also a predefined list) They select the item and I send the variable back the MainView.

-- Now can somebody explain to me (gently) why when I return to the main view the NSString variable that was previously holding the 'chosen' category is now null?

So my NSString selectedProject is not being retained correct? What is the correct implementation I should be doing here? Or what am I missing? I'm really trying to understand whats going on so anything would be a great help.

MainView Interface

    @interface MainViewController : UIViewController {
        NSString *selectedProjectName;
        NSString *selectedCategory;
    }

    @property (nonatomic, retain) NSString *selectedProjectName;
    @property (nonatomic, retain) NSString *selectedCategory;

    @end

MainView Implementation

    @implementation MainViewController
    @synthesize selectedCategory, selectedProjectName;

and if you need it.. ProjectView Implementation

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"masterSegue"]) {
    MainViewController *vc = (MainViewController *)[segue destinationViewController];

    LogValues *lv = [LogValues alloc];

    lv.project = @"Test Project Name";
    vc.selectedProjectName = lv.project;
    } 
    }
Desh__
  • 899
  • 1
  • 10
  • 16
  • Probably because you're not "returning" to the main view, you're probably creating a new instance. How do you go "back" to the main view (what type of segue)? – rdelmar May 16 '13 at 02:58
  • returning back I have the segue set to modal – Desh__ May 16 '13 at 03:21

2 Answers2

0

Ok, your problem is that you're not returning to the same instance. You've missed one very very important thing about segues -- they ALWAYS instantiate new view controllers. So, when you use a modal segue to go "back" to the main view controller, you're just creating a new instance of that controller. You should never go backwards (to earlier controllers) in a storyboard using anything other than an unwind segue. Unwinds are the exception to the rule about segues always creating new instances.

So, you have 2 choices. You can either use an unwind segue, which is nice, because you can still implement prepareForSegue to send data back to the destination controller, or you can not use a segue at all, and use dismissViewControllerAnimated:completion: to go back, and use a delegate to send back the data.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • If you don't know how to make an unwind segue, see my answer here. http://stackoverflow.com/a/16160239/1285133 – rdelmar May 16 '13 at 03:38
  • That's great man, appreciate the explanation. I'll look into using unwinds, I haven't heard of those yet, somehow. – Desh__ May 16 '13 at 03:39
-1

So of course immediately after posting I see a thread in the side that I believe helps. The solution was to store the values in the AppDelegate which is working fine.

I feel however there are other ways to achieve the same result without having to rely on the AppDelegate and would love to hear other solutions.

Desh__
  • 899
  • 1
  • 10
  • 16
  • So if this is a bad way to go, even though it works fine - Would you mind giving an explanation as to the more proper way. It's a small inhouse application only 8 variables to store so I dont see the harm doing it this way. Just trying to learn the best. – Desh__ May 17 '13 at 03:44