Passing NSString returning null in next ViewController

人盡茶涼 提交于 2019-12-02 19:34:09

问题


I am trying to pass an NSString from one view controller to the next. However I cannot seem to get it to function properly and I have set up a simple NSLog(@"%@", myString); before I try to [vc2 setString:myString]; which prints the correct string, and also in the second viewController and it is coming back as null so clearly I am doing something wrong. Here is my code.

first 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

Second 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

回答1:


I'm guessing from the absence of a push to the second VC that you're using a segue in a storyboard to go from the first to the second.

If this is correct then you should do something glide this in the first view controller...

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

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

And remove the lines that deal with VC2 in viewDidLoad.




回答2:


Where does the created VideoController is used? In your post it's getting created but never used - you must use the same created (and assigned) instance in order to see the right value...



来源:https://stackoverflow.com/questions/22959716/passing-nsstring-returning-null-in-next-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!