0

I have two view controllers inside a Navigation Controller. In the first view controller I have two buttons. Both of them call the second view controller using a Push segue, but:

  1. I need to know which button sent me in the second view controller. How?
  2. In the second view controller I have a UIDatePicker and a Button "Ok": how can I send the chosen date to the first view controller when Ok is pressed? (And how do I receive them?)

EDIT:

I don't know if my problem is clear: now I know how to pass data from the first view controller to the second view controller with prepareForSegue, but what I really need is to pass data (the picked date) from the second view controller to the first, and how can I do it without a prepareForSegue (when Ok is pressed)?

EDIT2:

I made it. It was so simple, guys... I decided to use modal segue:

Firstviewcontroller.h:

+(FirstViewController *)getInstance;

Firstviewcontroller.m:

static FirstViewController *instance =nil;
+(FirstViewController *)getInstance
{
    return instance;
}

and in its ViewDidLoad:

instance = self;

Secondviewcontroller.m, in the OkButton IBAction:

SecondViewController *secondViewController = [SecondViewController getInstance];

//...
//modify what I need to modify in secondviewcontroller
//...

[self dismissModalViewControllerAnimated:YES];

That's it. Thank you all anyway.

Ales
  • 19
  • 8
  • Show me your code how you push.. – TamilKing May 15 '14 at 07:16
  • I just dragged and drop with storyboard and chose "Push"... – Ales May 15 '14 at 07:17
  • in your first view controllers prepareForSegue method you can add informations to your second view controller so you can determine which button has been pushed. and than use the key value observing, or simply use your first view controller as a delegate in the second view controller, to push data back to the first view – Argent May 15 '14 at 07:19
  • I'm still so confused and I can't make it work. Maybe for my case a modal segue is the best? – Ales May 15 '14 at 09:29

2 Answers2

3

Assign Identifier to each segue in storyboard and implement

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];
        [vc setDelegate:self];
        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}  

For more info about How to use storyboard and pass value check this article or this discussion on stackoverflow

for the second question you can use delegate pattern IN SecondViewController.h

@protocol SomethingDelegate <NSObject>
- (void)dateChanged:(NSString *)dateStr; //you can use NSDate as well

@end
@interface ViewController2 : UIViewController

@property(weak) id<SomethingDelegate> delegate;

@end 

in .m file

-(void) OkClicked{

    [_delegate dateChanged:@"YOUR_DATE_VALUE"];
}

In FirstViewController.h

#import "SecondViewController.h"
@interface FirstViewController : UIViewController<SomethingDelegate>

in .m

  -(void)dateChanged:(NSString *)dateStr{

        // do whatever you need with dateStr
//also i made some change in prepareForSegue method
    }

Note:- take care your naming convenes for VC

Community
  • 1
  • 1
Bharat
  • 2,987
  • 2
  • 32
  • 48
  • 1
    This is the best way to pass some objects in segue based app. +1 for clear answer. – Blios May 15 '14 at 07:24
  • When you write YourViewController you mean the first or the second? And if it's the second, I have to import secondviewcontroller.h, right? – Ales May 15 '14 at 07:24
  • implement this method in your first VC and `YourViewController` in my answer is refer to your second ViewController. – Bharat May 15 '14 at 07:26
  • I'v edited my answer, let me know in case you need more help. – Bharat May 15 '14 at 07:36
  • One question: in my second view controller, do I have to do another Push Segue between my Ok button and the first view controller? Basically I want that when I press the button, I pass data to my FirstViewController and I want to come back to it. – Ales May 15 '14 at 07:41
0
  • just pass the button id to the second viewcontrol.
  • use delegates to sent the data from second viewcontroller back to first view controller

regards

Johan

baliman
  • 588
  • 2
  • 8
  • 27