简体   繁体   English

如何使用两个Push Segue在导航控制器中的两个View Controller之间传递数据?

[英]How can I pass data between two View Controllers in a Navigation Controller, with two Push Segue?

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: 他们两个都使用Push segue调用第二个视图控制器,但是:

  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? 在第二个视图控制器中,我有一个UIDatePicker和一个“确定”按钮:当按下“确定”时,如何将所选日期发送到第一个视图控制器? (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)? 我不知道我的问题是否很清楚:现在我知道如何使用prepareForSegue将数据从第一个视图控制器传递到第二个视图控制器,但是我真正需要的是从第二个视图控制器传递数据(选择的日期)到第一个,如何在没有prepareForSegue的情况下(按OK时)进行操作?

EDIT2: 编辑2:

I made it. 我做到了。 It was so simple, guys... I decided to use modal segue: 太简单了,伙计们...我决定使用模式搜索:

Firstviewcontroller.h: Firstviewcontroller.h:

+(FirstViewController *)getInstance;

Firstviewcontroller.m: Firstviewcontroller.m:

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

and in its ViewDidLoad: 并在其ViewDidLoad中:

instance = self;

Secondviewcontroller.m, in the OkButton IBAction: Secondviewcontroller.m,在OkButton IBAction中:

SecondViewController *secondViewController = [SecondViewController getInstance];

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

[self dismissModalViewControllerAnimated:YES];

That's it. 而已。 Thank you all anyway. 还是谢谢你。

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 有关如何使用情节提要和传递值的更多信息, 请查看本文有关stackoverflow的讨论

for the second question you can use delegate pattern IN SecondViewController.h 对于第二个问题,您可以在SecondViewController.h使用delegate pattern

@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 在.m文件中

-(void) OkClicked{

    [_delegate dateChanged:@"YOUR_DATE_VALUE"];
}

In FirstViewController.h FirstViewController.h

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

in .m 在.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 注意:-请注意您的命名大会

  • just pass the button id to the second viewcontrol. 只需将按钮ID传递给第二个viewcontrol。
  • use delegates to sent the data from second viewcontroller back to first view controller 使用委托将数据从第二个ViewController发送回第一个View Controller

regards 问候

Johan 约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM