BriOnH
BriOnH

Reputation: 945

Does unwinding a storyboard (exit) segue replace the need for a child delegate in a parent scene?

Does unwinding a storyboard segue in ios6 replace the need to make a source scene implement a delegate to pass data back from the child scene to the parent scene in ios5?

The way I usually do it is:

Parent Controller Header: Call the Delegate of the child scene

@interface ParentViewController : UIViewController <ChildViewControllerDelegate>
//ok not much to show here, mainly the delegate
//properties, methods etc
@end

Parent Controller Main (body): Prep the segue, set the delegate, create a return method from child scene

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

   if ([[segue identifier] isEqualToString:@"toChildScene"])
   {
       UINavigationController *childViewController = segue.destinationViewController;
       childViewController.delegate = self;
   }
}

#pragma mark - Delegate Segue Methods

-(void) childViewControllerDidSave: (ChildViewController *) controller Notes:(NSString *)sNotes
{
   someTextLabel.Text = sNotes
   [self dismissModalViewControllerAnimated:YES];    
}

Child Controller Header: create the delegate, reference the parent scenes methods

@class ChildViewController;

@protocol ChildViewControllerDelegate <NSObject>
-(void) childViewControllerDidSave: (ChildViewController *) controller Notes:(NSString *)sNotes
@end

@interface ChildViewController : UIViewController 
@property (weak, nonatomic) id <ChildViewControllerDelegate> delegate;
//properties, methods, etc
@end

Child Controller Main (body): call the parent scenes method

- (IBAction)someAction:(id)sender
{
   [self.delegate childViewControllerDidSave:self sNotes:someTextField.text];
}

So now the million Dollar question: Is this process now simpler in iOS 6? Can I cut a lot of the work out using unwinding a segue / exit segue? Any example would be greatly appreciated.

Upvotes: 2

Views: 1248

Answers (1)

Bryan Luby
Bryan Luby

Reputation: 2567

Yes.

Unwind segues are an abstracted form of delegation. In iOS 6, it's simpler to use unwinds rather than delegates to pass data backwards when dismissing view controllers.

In the parent view controller, create an unwind method that returns an IBAction and takes a UIStoryboardSegue as the argument:

- (IBAction)dismissToParentViewController:(UIStoryboardSegue *)segue {
    ChildViewController *childVC = segue.sourceViewController;
    self.someTextLabel.Text = childVC.someTextField.text;
}

Then, in the child view controller, Control-drag from your dismiss button to the green exit icon to connect the unwind segue:

enter image description here

Upvotes: 4

Related Questions