0

My destination ViewController has a number of labels and images which need to be changed dependent on the specific previous ViewController. I have a segue set up which should change these but for some reason it is not, even though the segue is being called.

My initial ViewController has a button which has a segue passing to the next ViewController with the name "aName". I'm probably missing something simple but I have been following another section of my code which does the exact same thing and works! I have also tested that the segue is successfully being called with a NSLog which returns fine. All the IBOutlets are also assigned correctly.

Initial View Controller.m:

#import "dViewController.h"

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"aName"]) {
            dViewController *dVC = (dViewController *)segue.destinationViewController;
            dVC.TitleLabel.text = @"abc";
            dVC.1Img.image = [UIImage imageNamed:@"somepath.jpg"];
            dVC.2Img.image = [UIImage imageNamed:@"somepath2.jpg"];
            [dVC.1Button setTitle:@"abcd" forState:UIControlStateNormal];
            [dVC.1Button setTitle:@"efgh" forState:UIControlStateNormal];

        }

Destination ViewController:

dViewController.h

    @property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
    @property (strong, nonatomic) IBOutlet UIImageView *1Img;
    @property (strong, nonatomic) IBOutlet UIImageView *2Img;
    @property (strong, nonatomic) IBOutlet UIButton *1Button;
    @property (strong, nonatomic) IBOutlet UIButton *2Button;

Edit: From doing some more looking around I think I should be using a delegate. It seems quite complicated and not really sure where I start. But I'll have a go at looking at what's in this thread: Changing label's text in another view controller

Community
  • 1
  • 1
user3728778
  • 117
  • 3
  • 8

3 Answers3

3

you have to create a variable for each label you want to change and in viewDidLoad assing the variable to the label.text

for example

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"aName"]) {
        dViewController *dVC = (dViewController *)segue.destinationViewController;
        dVC.titleString = @"abc";
        dVC.1Image = [UIImage imageNamed:@"somepath.jpg"];
        dVC.2Image = [UIImage imageNamed:@"somepath2.jpg"];
        dVC.1ButtonString=@"ButtonTitle";
        dVC.2ButtonString=@"ButtonTitle2";

    }

in DestinationController

    @property (strong, nonatomic) IBOutlet UILabel *TitleLabel;    
    @property (strong, nonatomic) IBOutlet UIImageView *1Img;      
    @property (strong, nonatomic) IBOutlet UIImageView *2Img;      
    @property (strong, nonatomic) IBOutlet UIButton *1Button;  
    @property (strong, nonatomic) IBOutlet UIButton *2Button;  

    @property(strong,nonatomic)NSString *2ButtonString;  
    @property(strong,nonatomic)UIImage*1Image;  
    @property(strong,nonatomic)UIImage*2Image;  
    @property(strong,nonatomic)NSString* titleString;  
    @property(strong,nonatomic)NSString *1ButtonString;  
    @property(strong,nonatomic)UIImageView *1Img;  
    @property(strong,nonatomic)UIImageView *1Img;  



 - (void)viewDidLoad{

   self.TitleLabel.text = self.titleString;

   self.1Img.image = self.1Image;

   self.2Img.image = self.2Image;

   [self.1Button setTitle:self.1ButtonString];

   [self.2Button setTitle:self.1ButtonString];

}

hope It help you, the problem is that with

 dViewController *dVC = (dViewController *)segue.destinationViewController; 

you only bring the controller not the view. and the IBOutlet belongs to the view and the view is not load i hope you understand

Klinkert0728
  • 326
  • 1
  • 4
  • 14
  • That worked, exactly how I wanted. I'm not sure if everyone else understood what I meant. But this explanation makes sense to why it was happening in the first place. – user3728778 Jul 03 '14 at 00:25
0

Put parentheses around segue.destinationViewController like this:

        dViewController *dVC = (dViewController *)(segue.destinationViewController);
JackyJohnson
  • 3,106
  • 3
  • 28
  • 35