2012-04-29 74 views
0

我是Xcode与故事板编程的新手。 我想从“SecondViewController”传递一个值(标签文本)到“视图控制器”。 这里是我的代码:在两个视图之间传递值 - Xcode 4.2与故事板

ViewController.m

- (IBAction)showSecondView:(id)sender { 
    NSLog(@"Trying.."); 
    // The identifier used here is set on the second view controller in the UIStoryboard. 
    SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondIdentifier"]; 
    NSLog(@"Test: %@",svc.labelTest.text); //this print NIL!! 
    svc.labelTest.text = @"GOOFY!!"; 
    [self.navigationController pushViewController:svc animated:YES]; 
} 

SecondViewController.h

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController 

- (IBAction)returnToFirstView:(id)sender; 
- (IBAction)toTabBar:(id)sender; 
@property (strong, nonatomic) IBOutlet UILabel *labelTest; 

@end 

SecondViewController.m

#import "SecondViewController.h" 

@implementation SecondViewController 
@synthesize labelTest; 

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

有人可以帮助我吗? 非常感谢!

斯特凡诺

回答

1

,如果你使用的是故事板,你为什么不使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

取代你- (IBAction)showSecondView:(id)sender { - 方法

有:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
if ([segue.identifier isEqualToString:@"SecondIdentifier"]) { 
SecondViewController *svc = (SecondViewController *)segue.destinationViewController; 
svc.labelTest.text = @"Goofy"; 
} 
+0

谢谢Schnarchii!我解决了我的问题! – 2012-05-01 10:10:51

+0

没问题:)如果它帮助不接受答案不适合我,但其他。所以如果他们有类似的问题,他们可以选择最好的解决方案。它帮助你和stackoverflow .. – Quirin 2012-05-01 10:30:16

相关问题