0

I'm developing my first iOS app and I've been trying to adhere for good MVC practices and laid out conventions to make me app easy to maintain and redevelop. I'm making use of a Storyboard and largely I will use Push segues to navigate through my app. Here is a snapshot of my Storyboard.

enter image description here

When Add Staff is clicked, a push segue moves to Select Staff. Once on Select Staff, I select the staff I want and can return to the previous VC. I should mention that each VC has it's own .m and .h which I subclassed from UIViewController. Here is what it looks like when used.

enter image description here

Now I've hit a brick wall in transferring the selected data rows back to the first VC. My problem is that I don't know how to best transfer data back to a VC from one which has been pushed by it. In the Select Staff VC, I have an array (currentSelected) which correctly holds the Strings of the selected rows, but I want to access this array in the Create VC once I have rolled back.

How do I best go about doing this?

P.S. It's worth noting that I realise there are similar questions to this one, but they are either for slightly varying issues, slightly complex or folder older versions of XCode which talk about XIBs and stuff. For the past while I've been researching the literature on the issue and can't make effective headway, so I hope this explains why I made this question.

Geesh_SO
  • 2,156
  • 5
  • 31
  • 58

1 Answers1

4

You can use block or delegate. I personally recommended a block because you keep your code grouped logically. With block you have to do something like that. In your SelectStaff.h file add typedef before interface:

// I use NSString as an example but for your project more accurate would be NSArray
typedef void (^ReturnBlock)(NSString *arg);

After that in @interface but before @end add Property:

@property(copy) ReturnBlock returnBlock;

In SelectStaff.m when you have your data (maybe in your save: method or dismiss:) Call the block with the data you want to pass;

if (returnBlock)
   returnBlock(@"Data will be passed do previous view controller");

In your CreateViewController.m in prepareForSegue: add block:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"YOUR_IDENT_FROM_STORYBOARD"])
    {
        SelectStaff *selStuff = (SelectStaff*)[segue destinationViewController];
        [selStuff setReturnBlock:^(NSString *arg){
            // This are your data 
            NSLog(@"Arg: %@", arg);
        }];
    }
}

Hope this help.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Is there an API which gives me a function which would be called just before I unwind to the first VC? So for example, I've selected my entries, and I click the back button. But before the VC is dismissed, that function is called and I can perform the `if(returnBlock)...` and then the VC is dismissed. – Geesh_SO Dec 11 '13 at 15:04
  • In your CreateViewController you can add method to unwind segue, for example: - (IBAction)unwindToRed:(UIStoryboardSegue *)unwindSegue{SelectStaff* selectStaff = unwindSegue.sourceViewController; } and you can read the data you need,you don't even need block. Just remember drag from exit icon to this action in storyboard. – Greg Dec 11 '13 at 15:10
  • Thanks for your advice, I appreciate it. I implemented the code you suggested, but I have this error, any suggestions? http://i.imgur.com/UbLpbnU.png – Geesh_SO Dec 11 '13 at 15:19
  • Have you added @property(copy) ReturnBlock returnBlock; to your DDSStaffTableVC.h file (.h it's important) and have you added #import "DDSStaffTableVC.h" to the file where you get the error? – Greg Dec 11 '13 at 15:24
  • Your second suggestion fixed it. I had put `@class DMSStaffTableViewController;`in the .h file but it only fixed when I put `#import 'DMSStaffTableViewController.h'`. Weird. Thank you! – Geesh_SO Dec 11 '13 at 15:33
  • Please specify where to add this code and where to call this block – Darshan Dec 22 '16 at 12:41