目标ViewController在segue之后不会改变

时间:2014-07-02 18:33:17

标签: ios objective-c uiviewcontroller

我的目标ViewController有许多标签和图片,需要根据特定的ViewController进行更改。我有一个segue设置应该改变这些,但由于某种原因它不是,即使segue被调用。

我的初始ViewController有一个按钮,它有一个segue传递给下一个名为" aName"的ViewController。我可能遗漏了一些简单的东西,但我一直在关注我的代码的另一部分,它完全相同并且有效!我还测试了使用NSLog成功调用segue,返回正常。所有IBOutlet也被正确分配。

初始视图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];

        }

目的地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;

编辑:从做一些环顾四周我想我应该使用委托。这似乎很复杂,并不确定我从哪里开始。但我会仔细研究这个帖子中的内容:Changing label's text in another view controller

3 个答案:

答案 0 :(得分:3)

您必须为要更改的每个标签创建一个变量,并在viewDidLoad中为变量添加标签.text

例如

 - (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";

    }
目标控制器中的

    @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];

}

希望它对你有帮助,问题在于

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

你只带控制器而不是视图。并且IBOutlet属于视图,视图不加载我希望您理解

答案 1 :(得分:0)

将括号放在segue.destinationViewController周围,如下所示:

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

答案 2 :(得分:0)