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

时间:2014-04-09 10:25:40

标签: ios objective-c uiviewcontroller parameter-passing

我正在尝试将 NSString从一个视图控制器传递到下一个。但是我似乎无法让它正常运行,我在尝试之前设置了一个简单的NSLog(@"%@", myString); [vc2 setString:myString];打印正确的字符串,也在第二个viewController中,它返回null,所以显然我做错了。这是我的代码。

第一个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

2 个答案:

答案 0 :(得分:1)

我猜测,如果没有推动第二个VC,你在故事板中使用segue从第一个到第二个。

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue
{
    VideoController *vc2 = segue.destinationViewController;

    [vc2 setCam:self.city.camera];
}

并删除viewDidLoad中处理VC2的行。

答案 1 :(得分:0)

使用创建的VideoController在哪里?在你的帖子中它被创建但从未使用过 - 你必须使用相同的创建(和分配)实例才能看到正确的值...