使用故事板在 xcode 中的两个视图控制器之间传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15308059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:56:06  来源:igfitidea点击:

Passing data between two viewcontrollers in xcode using storyboard

objective-cxcodexcode-storyboard

提问by Casper

I am super new to programming in general. I am trying to make a very simple app.

我对编程非常陌生。我正在尝试制作一个非常简单的应用程序。

I am trying to pass data from one viewcontroller to another using a storyboard. On my first screen I have a text field and a button then after you press the button i want the second screen's label to be updated with whatever text you put in the text field.

我正在尝试使用故事板将数据从一个视图控制器传递到另一个视图控制器。在我的第一个屏幕上,我有一个文本字段和一个按钮,然后在您按下按钮后,我希望使用您在文本字段中输入的任何文本来更新第二个屏幕的标签。

Here is some of my code:

这是我的一些代码:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize secondviewData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{


    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

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

- (IBAction)passData:(UIButton *)sender {

//    SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

    self.secondviewData = secondviewData;
    secondviewData.passedValue = input.text;

//    [self presentViewController:second animated:YES completion:nil];

    homeLabel.text = input.text;

}
@end

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize passedValue;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{



    label.text = passedValue;

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

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

@end

Here's a link to my code: http://oberober.com/passingData/

这是我的代码的链接:http: //oberober.com/passingData/

So far the label on the second screen keeps going blank no matter what I enter on the first screen's text field.

到目前为止,无论我在第一个屏幕的文本字段中输入什么,第二个屏幕上的标签都会保持空白。

I feel like I am missing an important concept? Any tips or tutorials I can look at?

我觉得我错过了一个重要的概念?我可以查看任何提示或教程?

Thanks ahead of time, Casper

提前致谢,卡斯帕

回答by Richard Brown

You pass the data by using the prepareForSeguemethod of the first controller. Here's an example from one of my apps that shows a vehicle object being passed to the second controller.
CHRBodyViewControlleris the class of the second controller.

您使用prepareForSegue第一个控制器的方法传递数据。这是我的一个应用程序中的一个示例,它显示了一个车辆对象被传递到第二个控制器。
CHRBodyViewController是第二个控制器的类。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    CHRBodyViewController *body = [segue destinationViewController];
    body.vehicle = _vehicle;
    body.updated = YES;

}