2012-04-29 12 views
0

私はstoryboardを使ったXcodeプログラミングで初心者です。 "SecondViewController"から "View Controller"に値(ラベルのテキスト)を渡そうとしています。ここで が私のコードです:2つのビュー間で値を渡す - Xcode 4.2 with storyboard

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 { -method

を置き換えるを使用しない理由はストーリーボード、使用している場合:

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

をあなたにSchnarchiiをありがとう!私は私の問題を解決しました! –

+0

問題はありません:)それは私のためではなく、他の答えを受け入れるよりも助けている場合。だから、もし彼らが似たような問題を抱えていれば、彼らは最良の解決策を選ぶことができますそれはあなたとstackoverflowを助けた.. – Quirin

関連する問題