简体   繁体   中英

Destination ViewController not changing after segue

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

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

像这样在segue.destinationViewController周围加上括号:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM