新的View Controller上的iOS UILabel在使用segue时没有更新

时间:2012-03-02 17:02:15

标签: ios uilabel viewcontroller segue

我在第二个UILabel(descriptionOfProgram)上有一个ViewController(GraphViewController),需要在原始MVC发生segue时设置。

目前,在我的主要MVC中,我在prepareForSegue:中做了两件事:

  1. 我在GraphVewController中将@property NSString设置为我想要的字符串。
  2. 我通过调用GraphViewController setDescriptionLabel:中的函数将descriptionOfProgram的文本设置为等于该字符串。
  3. graphViewController出现在字符串上时,文本不在标签中。在测试期间,我在test中创建了一个名为GraphViewController的按钮,以便在按下时调用setDescriptionLabel。这很有效。关于发生了什么的任何想法?

    我在第二个MVC中包含GraphViewController.h.m,在主MVC中包含GraphingCalculatorViewController.m

    GraphViewController.h:

    //
    //  GraphViewController.h
    //  GraphingCalculator
    //
    //  Created by Graham Gaylor on 3/1/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface GraphViewController : UIViewController
    @property (nonatomic,copy) NSArray *program;
    @property (nonatomic,weak) NSString *description;
    @property (weak, nonatomic) IBOutlet UILabel *descriptionOfProgram;
    - (IBAction)test:(id)sender;
    - (void)setDescriptionLabel:(NSString *)description;
    
    @end
    

    GraphViewController.m:

    //
    //  GraphViewController.m
    //  GraphingCalculator
    //
    //  Created by Graham Gaylor on 3/1/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import "GraphViewController.h"
    #import "GraphView.h"
    
    
    @interface GraphViewController() <GraphViewDataSource>
    @property (nonatomic, weak) IBOutlet GraphView *graphView;
    
    @end
    
    @implementation GraphViewController
    
    @synthesize program = _program;
    @synthesize descriptionOfProgram = _descriptionOfProgram;
    @synthesize description = _description;
    @synthesize graphView = _graphView;
    
    - (void)setProgram:(NSArray *)program
    {
        _program = program;
        [self.graphView setNeedsDisplay]; // any time our Model changes, redraw our View
        self.graphView.dataSource = self; // setting graphView delegate GraphVC
    
    }
    
    - (IBAction)test:(id)sender {
        self.descriptionOfProgram.text = @"test";
        [self setDescriptionLabel:self.description];
    }
    
    - (void)setDescriptionLabel:(NSString *)description
    {
        NSLog(@"Got into setDescriptionLabel");
        self.descriptionOfProgram.text =  @"hello";
    }
    
    - (NSArray *)programForGraphView:(GraphView *)sender {
        return self.program;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }
    - (void)viewDidUnload {
        [self setDescriptionOfProgram:nil];
        [super viewDidUnload];
    }
    @end
    

    这是我的主要MVC。我摆脱了所有不涉及seque的额外功能。

    GraphingCalculatorViewController.m:

    //
    //  GraphingCalculatorViewController.m
    //  GraphingCalculator
    //
    //  Created by Graham Gaylor on 3/1/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import "GraphingCalculatorViewController.h"
    #import "GraphViewController.h"
    
    
    @interface GraphingCalculatorViewController()
    @property (nonatomic,strong) NSArray *programToGraph;
    @end
    
    @implementation GraphingCalculatorViewController
    
    @synthesize programToGraph = _programToGraph;
    
    - (void)setAndShowGraph:(NSArray *)program {
        self.programToGraph = program;
        [self performSegueWithIdentifier:@"ShowGraph" sender:self];
        NSLog(@"setAndShowGraph");
    }
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if([segue.identifier isEqualToString:@"ShowGraph"]) {
            [segue.destinationViewController setProgram:self.programToGraph]; // sets GraphVC's program to current program
            [segue.destinationViewController setDescriptionLabel:[CalculatorBrain descriptionOfProgram:self.programToGraph]];
            NSLog(@"prepareForSegue");
        }
    }
    - (IBAction)graphPressed {
        [self setAndShowGraph:self.brain.program];
    }
    
    - (void)viewDidUnload {
        [self setVariablesUsed:nil];
        [super viewDidUnload];
    }
    @end
    

1 个答案:

答案 0 :(得分:1)

prepareForSegue:之前调用

viewDidLoad:,因此您的IBOutlet将为零。尝试将传入值设置为NSString,然后使用该字符串填充viewDidLoad:上的标签