0

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?

Joe McBride
  • 3,789
  • 2
  • 34
  • 38
burrGGG
  • 617
  • 2
  • 9
  • 18
  • Log photoNumber. deleteObject in viewDidAppear. And check what value you get when you popViewController. – CRDave Jan 21 '15 at 13:46
  • Sorry, can you please explain a little more. I don't understand, if you could give me some sample code would be great – burrGGG Jan 21 '15 at 13:51
  • Just log value of photoNumber. deleteObject in viewDidAppear of first view controller. When you pop DiaryViewController viewDidAppear will be called and log will be printed. Check it is new value or not. – CRDave Jan 21 '15 at 13:53
  • It is returning null. I've also added break point in my prepareForSegue and it is not getting called. Something to do with it being the reverse of the original push? – burrGGG Jan 21 '15 at 14:03
  • prepareForSegue will no be called that is final and normal behaviour. Because segue and pop both are different. BTW Previous was my mistack Log "self.rowTodelete" – CRDave Jan 21 '15 at 14:05
  • I did try that, but still didn't work..Basically i just want to use a NSNumber from one class in another? I know it should be easy but i'm struggling – burrGGG Jan 21 '15 at 14:18

3 Answers3

1

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;
    }
}
Community
  • 1
  • 1
mbm29414
  • 11,558
  • 6
  • 56
  • 87
  • I am not dismissing the ViewController with the action from a UIButton, I am calling it when the button on UIAlertView is pressed, so i'm not sure how to connect it up without using storyboard and ctrl +drag from UIButton – burrGGG Jan 21 '15 at 13:49
  • You can call an unwind segue programmatically, too. – mbm29414 Jan 21 '15 at 14:01
0

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
Sebastian Boldt
  • 5,283
  • 9
  • 52
  • 64
  • I'm a bit confused. My source Controller would be my second ViewController? I don't really understand, where is the 1337 coming from? – burrGGG Jan 21 '15 at 15:17
  • No, your Source-Controller calls the segue .. which then presents the destinationController .. you set the SourceController inside prepareForSegue as your delegate of the destination Controller and then you can call a method from your Destination Controller which can receive data .. the 1337 is just dummy data i wanted to pass back to my delegate(Sourcecontroller) to show how the pattern works. – Sebastian Boldt Jan 21 '15 at 15:21
0

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
        };   
    }
}
timgcarlson
  • 3,017
  • 25
  • 52