1

Wondering how I can set properties of view controllers that are already on the NavigationController's stack

My situation:

I want to set up an image uploading flow like this

(Navigation Stack) RootViewController -> TakePictureViewController -> EditPictureViewController -> UploadPictureViewController

When user confirms the upload from the UploadPictureViewController, rather than start to upload, I want to set an NSDictionary property on RootViewController which contains the upload query, then pop the navigation stack back down to the RootViewController and have it handle initiating and status reporting of the query.

Here's my code in the uploadpictureviewcontroller, currently, the code does pop to the right view controller, but the uploadPackage property is still nil, also I have tried to -setUploadPackage

RootViewController *rvc = (RootViewController *)[self.navigationController.viewControllers objectAtIndex:0];

rvc.uploadPackage = uploadPackage;

[self.navigationController popToViewController:rvc animated:YES]; 

All help appreciated, thanks.

Doug
  • 859
  • 1
  • 9
  • 20

1 Answers1

2

try using [self.navigationController popToRootViewControllerAnimated:YES]. That should do it.

EDIT:

If you have only one instance of RootViewController, then you can set it up as a singleton and therefore you can access it from any other controller (just like the appDelegate). To do so you need to add the following to your RootViewController.m under synthesize...; :

static RootViewController *rootViewController;

+(id)sharedRootController {
    return rootViewController;
}

inside your init method for RootViewController add the following line:

rootViewController = self;

now back to your UploadPictureViewController you can set the uploadPackage like this:

RootViewController *rvc = [RootViewController sharedRootController];
rvc.uploadPackage = uploadPackage;

Please note that you should NOT use the singleton method if there is to be more than one instance of RootViewController.

hope this helps!

KDaker
  • 5,899
  • 5
  • 31
  • 44
  • Please see my answer edits, I am wondering how to report data back to the RootViewController. – Doug Aug 15 '12 at 17:26
  • alright, first of all did the `popToRootViewControllerAnimated:` work? atleast for popping? second of all, is it possible that there would be more than once instance of RootViewController in your application at any given time? – KDaker Aug 15 '12 at 17:53
  • The popping does work. Also, there shouldn't be. The RootViewController is basically the starting point of all the application's actions, and it never ever gets pushed, only popped to. – Doug Aug 15 '12 at 18:01
  • Cool, thanks for the tip. Seems to be up and running now. This method will be useful for a lot of other stuff on this project too... thanks! – Doug Aug 15 '12 at 18:19
  • "Tried to pop to a view controller that doesn't exist." got this error when I used this method. The only difference was instead of rootviewcontroller, I used my customViewController. It looks like it should work but it doesnt. Any ideas? – Esqarrouth May 06 '14 at 22:04