传递NSString在下一个ViewController中返回null

我想从一个视图控制器传递NSString到下一个 。 不过,我似乎无法得到它正常工作,我已经build立了一个简单的NSLog(@"%@", myString); 在我尝试[vc2 setString:myString]; 它打印正确的string,并在第二个viewController,它是回来为空如此清楚,我做错了什么。 这是我的代码。

第一个viewController

 #import "DetailController.h" #import "City.h" #import "VideoController.h" #import "Helper.h" @interface DetailController () @end @implementation DetailController @synthesize city, ClubName, Price, Vip, Promo, remain,p,deal,money,camera,cam,tweet,post; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; UIImage *highlightedButtonImage = [UIImage imageNamed:@"twitter.png"]; [Helper customizeBarButton:self.tweet image:highlightedButtonImage highlightedImage:highlightedButtonImage]; UIImage *faceButtonImage = [UIImage imageNamed:@"facebook.png"]; [Helper customizeBarButton:self.post image:faceButtonImage highlightedImage:faceButtonImage]; // Do any additional setup after loading the view. UIFont *labelFont=[UIFont fontWithName:@"Deutsch Gothic" size:20.0]; UIFont *myFont=[UIFont fontWithName:@"Deutsch Gothic" size:30.0]; UIFont *titleFont=[UIFont fontWithName:@"Deutsch Gothic" size:40.0]; NSString * name= self.city.clubName; NSString * line= self.city.clubLine; NSString * description= self.city.promo; NSString * price= self.city.price; remain.font=labelFont; remain.text=@"VIP Remaining :"; p.font=labelFont; p.text=@"Price :"; money.font=myFont; deal.font=labelFont; deal.text=@"Promotions :"; ClubName.font=titleFont; ClubName.text=name; Vip.font=myFont; Vip.text=line; Price.font=myFont; Price.text=price; Promo.font=labelFont; Promo.text=description; } - (IBAction)play:(id)sender { VideoController *vc2 = [[VideoController alloc]initWithNibName:@"ViewController" bundle:nil]; [vc2 setCam:self.city.camera]; [self.navigationController pushViewController:vc2 animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

第二个ViewController

 #import "VideoController.h" #import "City.h" @interface VideoController () @end @implementation VideoController @synthesize webView,city,cam; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { NSLog(@"%@", cam); image.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"32.tiff"], [UIImage imageNamed:@"31.tiff"], [UIImage imageNamed:@"30.tiff"], [UIImage imageNamed:@"29.tiff"], [UIImage imageNamed:@"28.tiff"], [UIImage imageNamed:@"27.tiff"], [UIImage imageNamed:@"26.tiff"], [UIImage imageNamed:@"25.tiff"], [UIImage imageNamed:@"24.tiff"], [UIImage imageNamed:@"23.tiff"], [UIImage imageNamed:@"22.tiff"], [UIImage imageNamed:@"21.tiff"], [UIImage imageNamed:@"20.tiff"], [UIImage imageNamed:@"19.tiff"], [UIImage imageNamed:@"18.tiff"], [UIImage imageNamed:@"17.tiff"], [UIImage imageNamed:@"16.tiff"], [UIImage imageNamed:@"15.tiff"], [UIImage imageNamed:@"14.tiff"], [UIImage imageNamed:@"13.tiff"], [UIImage imageNamed:@"12.tiff"], [UIImage imageNamed:@"11.tiff"], [UIImage imageNamed:@"10.tiff"], [UIImage imageNamed:@"9.tiff"], [UIImage imageNamed:@"8.tiff"], [UIImage imageNamed:@"7.tiff"], [UIImage imageNamed:@"6.tiff"], [UIImage imageNamed:@"5.tiff"], [UIImage imageNamed:@"4.tiff"], [UIImage imageNamed:@"3.tiff"], [UIImage imageNamed:@"2.tiff"], [UIImage imageNamed:@"1.tiff"],nil]; [image setAnimationRepeatCount:1]; image.animationDuration = 10.0; [image startAnimating]; [super viewDidLoad]; } -(void)yourMethod { [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

我猜从没有推到第二个VC,你在一个故事板上使用一个segue从第一个到第二个。

如果这是正确的,那么你应该做的东西在第一个视图控制器中滑动…

 - (void)prepareForSegue:(UIStoryboardSegue *)segue { VideoController *vc2 = segue.destinationViewController; [vc2 setCam:self.city.camera]; } 

并删除viewDidLoad中处理VC2的行。

在哪里使用创build的VideoController? 在你的文章中,它已经被创build,但从来没有使用过 – 你必须使用相同的创build(和分配)的实例,才能看到正确的值…