burrGGG
burrGGG

Reputation: 617

Pass NSNumber back through segue

I have used prepareForSegue successfully, which I have managed to pass variables though. I am trying to now pass a NSNumber through the reverse of the segue but prepareForSegue is not getting called. To get back to my previous VC I am using:

[self.navigationController popViewControllerAnimated:YES];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{    
    if ([[segue identifier] isEqualToString:@"ShowImages"]) {
       DiaryViewController  *photoNumber = [segue destinationViewController];
       photoNumber.deleteObject = self.rowTodelete;   
}

Is there something I can add to make the prepareForSegue to work in reverse. Or would I need to access my NSNumber from the different class somehow?

Upvotes: 0

Views: 122

Answers (3)

timgcarlson
timgcarlson

Reputation: 3146

Another method that could work for you is assigning a completion block to run when the presented view is dismissed. For example, if A presents B with a segue, then set a completion block when you initially segue to B. When B is about to be dismissed, run the completion block.

In ViewControllerB.h

@property (nonatomic, strong) void (^onDismiss)(UIViewController *sender, NSNumber *aNumber);

In ViewControllerB.m

// execute the block right before A is presented again
- (void)viewWillDisappear:(BOOL)animated {
    self.onDismiss(self, aNumberToPassBack);  // Run the block 
}

In ViewControllerA.h

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"SegueToB"]) {
        ViewControllerB *vcb = [segue destinationViewController];

        vcb.onDismiss = ^(UIViewController *sender, NSNumber *aNumber) {
            self.aNumber = aNumber;
            // ... update your views if needed
        };   
    }
}

Upvotes: 0

Sebastian Boldt
Sebastian Boldt

Reputation: 5321

You could also use the Delegate-Pattern for it. Just set the current Controller as the Delegate of the destination controller and create a protocol that defines which method the delegate should be implement. Then you can pass any parameters you want back to the source Viewcontroller if your destination controller gets dismissed.

// Source Controller (which is also the delegate of the destination-controller)
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      SomeController * a = segue.destinationController;
      a.delegate = self;
}
// Delegate Method 
-(void)viewControllerGotDismissed:(NSNumber*)test{
      NSlog(@"TEST:%@",test);
}

// Destination Controller
@protocol SomeProtocol
   -(void)viewControllerGotDismissed:(NSNumber*)test;
@end

// Destination Controller
-(void)viewWillDisappear {
   // ....
  [self.delegate viewControllerGotDismissed:@1337]; // Pass the value to the source Controller
}
@end

Upvotes: 0

mbm29414
mbm29414

Reputation: 11598

If you set up an unwind segue, you can get the UIViewController that is causing the unwind.

Check out this answer here about Unwind Segues.

Once you actually set up the unwind segue structure, here's some sample unwind segue code:

- (IBAction)unwindToRed:(UIStoryboardSegue *)unwindSegue {
    UIViewController* sourceViewController = unwindSegue.sourceViewController;
    if ([sourceViewController isKindOfClass:[DiaryViewController class]] {
    DiaryViewController *photoNumber = (DiaryViewController *)sourceViewController;
    NSNumber *deleteObject = photoNumber.deleteObject;
    }
}

Upvotes: 1

Related Questions