2013-03-09 35 views
1

我对编程一般都很陌生。我正在尝试制作一个非常简单的应用程序。使用故事板在xcode中的两个视图控制器之间传递数据

我想将数据从一个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

搜索是你的朋友:http://stackoverflow.com/search?q=ios+pass+data+between+storyboard – rmaddy 2013-03-09 06:45:31

回答

4

通过使用第一控制器的prepareForSegue方法传递数据。下面是我的一个应用程序示例,显示车辆对象被传递给第二个控制器。
CHRBodyViewController是第二个控制器的类。

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

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

} 
+0

谢谢!现在我的第二个屏幕更新=] – Casper 2013-03-09 19:43:01

相关问题