我试图将一个字符串变量从 mainVC 类传递给 detailVC 类,但是当 viewDidLoad 启动时,myStringLabel 是空的,它应该包含“只是一个测试”。我不知道为什么。
主VC.m
#import "mainVC.h"
@implementation mainVC
@synthesize
-(IBAction) openSecondController {
//Ouverture de DetailVC
detailVC *dVC = [[detailVC alloc] init];
dVC.myStringLabel=@"Just a test";
NSLog(@"dVC.myStringLabel before calling dVC :%@", dVC.myStringLabel);
[self performSegueWithIdentifier:@"progDetailSegue" sender:nil];
NSLog(@"dVC.myStringLabel after calling dVC :%@", dVC.myStringLabel) ;
}
细节VC.h
#import <UIKit/UIKit.h>
@interface detailVC : UIViewController {
NSString *myStringLabel;
IBOutlet UILabel *labelForString;
}
@property (nonatomic, strong) UILabel *labelForString;
@property (copy) NSString *myStringLabel;
详细VC.m
#import "detailVC.h"
@implementation detailVC
@synthesize myStringLabel, labelForString;
-(void) viewDidLoad {
NSLog(@"myStringLabel in viewDidLoad : %@", dVC.myStringLabel) ;
labelForString.text=myStringLabel;
}
-(IBAction) closeViewController {
//Fermeture com_mobilizProgramDetailViewController ouverte en Modal.
[self dismissViewControllerAnimated:YES completion:nil];
}
NSLOG 输出:
2012-12-17 11:48:58.520 DataTransfert[5068:f803] dVC.myStringLabel before calling dVC : Just a test
2012-12-17 11:48:58.520 DataTransfert[5068:f803] myStringLabel in viewDidLoad : (null)
2012-12-17 11:48:58.520 DataTransfert[5068:f803] dVC.myStringLabel after calling dVC :Just a test