我一般都是编程的新手。我想做一个非常简单的应用程序。
我正在尝试使用故事板将数据从一个viewcontroller传递到另一个viewcontroller。在我的第一个屏幕上,我有一个文本字段和一个按钮,然后按下按钮后,我希望第二个屏幕的标签更新为您在文本字段中放置的任何文本。
以下是我的一些代码:
#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
以下是我的代码的链接:http://oberober.com/passingData/
到目前为止,无论我在第一个屏幕的文本字段输入什么内容,第二个屏幕上的标签都会变为空白。
我觉得我错过了一个重要的概念?我可以看一下任何提示或教程吗?
提前谢谢, 卡斯帕
答案 0 :(得分:4)
您使用第一个控制器的prepareForSegue
方法传递数据。以下是我的一个应用程序示例,其中显示车辆对象被传递给第二个控制器
CHRBodyViewController
是第二个控制器的类。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CHRBodyViewController *body = [segue destinationViewController];
body.vehicle = _vehicle;
body.updated = YES;
}