如何使用 unwind Segue 将数据从第二个视图控制器传递回第一个视图控制器?

How do I use an unwind Segue to pass data back from the second view controller to the first view controller?

我创建了两个视图控制器。第一个视图控制器向第二个视图控制器发送一个名称,该名称用于问候用户并提示用户以英寸为单位输入身高。

我遇到的问题是将高度信息从第二个视图控制器发送回第一个视图控制器,这样我就可以对数据进行转换,以便在第一个视图控制器中显示它视图控制器。

在我的第二个视图控制器(.h 文件)中,我创建了一个 属性,我正在使用我用于发送的 prepareForSegue 方法中的目标方法(我认为它是一个方法)将 nameField 信息传递给第二个视图控制器。它看起来像这样:

属性 在 SecondSceneViewController.h

#import <UIKit/UIKit.h>

//specifying delegate protocol

@interface SecondSceneViewController : UIViewController
@property (strong, nonatomic) NSString *labelText;

@end

我的 FirstSceneViewController.m 文件中的 prepareForSegue 方法如下所示:

#import "FirstSceneViewController.h"

@interface FirstSceneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UIButton *goToScene2Btn;
@property (weak, nonatomic) IBOutlet UITextField *displayField;
@property (weak, nonatomic) NSString *heightInches;

@end

@implementation FirstSceneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //bring data back from scene2
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondSceneViewController *destination = [segue destinationViewController];
    destination.labelText = self.nameField.text;
}

-(IBAction)goBackToScene1:(UIStoryboardSegue *)segue
{
    //Hello, Adnan, you are 6'3 which is 191cm
    self.displayField.hidden = NO;
    self.displayField.text = [NSString stringWithFormat:@"Hello, %@, you are %@ inches", self.nameField.text, self.heightInches]; //EXAMPLE OUTPUT
}


@end

这就是我的 SecondSceneViewController.m 文件将名称数据向前推进的方式:

#import "SecondSceneViewController.h"

@interface SecondSceneViewController ()
@property (weak, nonatomic) IBOutlet UILabel *greetingLabel;
@property (weak, nonatomic) IBOutlet UITextField *heightField;


@end

@implementation SecondSceneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //display name collected from scene1 held as strong var labelText
    self.greetingLabel.text = [NSString stringWithFormat:@"Hello, %@", self.labelText];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



@end

我读过的内容建议使用协议和委托在我的 FirstSceneViewController 中使用 unwind 方法使第一个场景成为第二个场景的委托,以便将高度数据带回第一个场景。

我是否应该在第二个场景视图控制器中设置一个动作来获取高度数据,以便将其设置为 labelText,以便我可以在第一个场景视图控制器中通过标签文本使用数据?

我在想这样的事情:

self.labelText = self.heightField.text;

然后,我在想既然 labelText 现在保存了第二个场景的数据,我可以只使用 viewDidLoad 方法来获取 labelText 数据并将其放入第一个场景视图控制器中的变量中像这样:

self.heightInches = self.labelText;

不幸的是,上面的策略没有奏效...

我已经尝试了有关堆栈溢出的每个相关问题的解决方案,但我已经走到了死胡同,我不理解这个过程是如何工作的。

任何人都可以提供一个可能的策略来帮助我找出这个解决方案吗?

如果你想重现问题,这里是 link 项目:

https://www.dropbox.com/s/g69i13uh0e1q5e9/2658.ICW01.zip?dl=0

我正在尝试使用 unwind segue 将数据传回第一个视图控制器。有谁知道如何正确实施此功能?

我的 goBackToScene1 是我的放松转场。我遇到的主要问题是使用这个 unwind segue 将数据从第二个场景发送回第一个场景。我使用变量 labelText 将数据从第一个场景向前发送到第二个场景,但现在我正在努力找出如何使用相同的 unwind segue 将其发回的正确语法。

如何使用名为 goBackToScene1 的展开转场将数据从第二个视图控制器发送回第一个视图控制器?

*更新***更新***更新*

所以我继续尝试解决这个问题,这是我到目前为止所做的:

  1. 我从同一个按钮创建了辅助连接,该按钮触发了我的 SecondSceneViewController.m 文件中的操作。

这是从故事板建立的联系的图片:

此操作的代码将我的 SecondSceneViewController.h 文件中定义为 属性 的 labelText 变量设置为用户输入的 heightInches 数据。这是我的 SecondSceneViewController.m 文件的代码,我在其中尝试使用 heightField 和 sendHeightToDestinationBtn 操作来操作 labelText 中的数据:

#import "SecondSceneViewController.h"

@interface SecondSceneViewController ()
@property (weak, nonatomic) IBOutlet UILabel *greetingLabel;
@property (weak, nonatomic) IBOutlet UITextField *heightField;


@end

@implementation SecondSceneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //display name collected from scene1 held as strong var labelText
    self.greetingLabel.text = [NSString stringWithFormat:@"Hello, %@", self.labelText];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendHeightToDestinationBtn:(UIButton *)sender {
    self.labelText = self.heightField.text;
}

这是我的 SecondSceneViewController.h 文件中的代码,显示了我的 labelText @属性:

@interface SecondSceneViewController : UIViewController
@property (strong, nonatomic) NSString *labelText;

@end
  1. 然后我在我的 FirstSceneViewController 中重写了我的 prepareForSegue 操作中的代码,以便在我调用的 unwind segue 中相应地使用它:goBackToScene1。这是我的 FirstSceneViewController.m 文件的代码:

    进口"FirstSceneViewController.h"

    @interface FirstSceneViewController() @属性 (weak, nonatomic) IBOutlet UITextField *nameField; @属性 (weak, nonatomic) IBOutlet UIButton *goToScene2Btn; @属性 (weak, nonatomic) IBOutlet UITextField *displayField; @属性 (weak, nonatomic) NSString *heightInches;

    @end

    @implementation FirstSceneViewController

    • (void)viewDidLoad { [超级viewDidLoad]; // 在加载视图后做任何额外的设置,通常是从 nib。 //从scene2取回数据 }

    • (void)didReceiveMemoryWarning { [超级 didReceiveMemoryWarning]; // 处理任何可以重新创建的资源。 }

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 如果([segue.sourceViewController isKindOfClass:[FirstSceneViewController class]]) { SecondSceneViewController *destination = [segue destinationViewController]; destination.labelText = self.nameField.text; } else if ([segue.sourceViewController isKindOfClass:[SecondSceneViewController class]]) { SecondSceneViewController *source = [segue destinationViewController]; self.heightInches = source.labelText; }

    }

    -(IBAction)goBackToScene1:(UIStoryboardSegue *)segue { //你好,阿德南,你身高 6 英尺 3 英寸,也就是 191 厘米 self.displayField.hidden = 否;

    self.displayField.text = [NSString stringWithFormat:@"Hello, %@, you are %@ inches", self.nameField.text, self.heightInches]; //EXAMPLE OUTPUT
    

    }

    @end

如您所见,我正在尝试使用 prepareForSegue 来确定何时使用我正在尝试使用的 labelText 变量,以便来回传递我需要的数据,但我只能让它工作第一个视图控制器到第二个而不是相反。

  1. 这里是 unwind segue 完成它的工作后的输出。

正如你从输出中看到的,我试图从第二个场景传递的数据,这是我试图通过 labelText 从 SecondSceneViewController 输出中的 heightField 传递到(null)的 heightInches 变量...

如何在这个变量中获取正确的数据???我知道我没有根据输出正确使用 unwind segue ...

如何找到正确解决这个问题的策略???

请帮忙!

谢谢。

你快搞定了。你有 unwind segue 方法,你只是没有使用它。您可以通过传递给您的展开方法的 segue 的 sourceViewController 属性 访问第二个视图控制器:

-(IBAction)goBackToScene1:(UIStoryboardSegue *)segue
{
    //Hello, Adnan, you are 6'3 which is 191cm
    self.displayField.hidden = NO;
    SecondSceneViewController *source = (SecondSceneViewController *)segue.sourceViewController;
    self.heightInches = source.heightField.text
    self.displayField.text = [NSString stringWithFormat:@"Hello, %@, you are %@ inches", self.nameField.text, self.heightInches]; //EXAMPLE OUTPUT
}