2012-04-29 76 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

相關問題