新视图控制器上的iOS UILabel在使用segue时未更新



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

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

  1. 我在GraphVewController中将一个@property NSString设置为我想要的字符串
  2. 我通过调用GraphViewController setDescriptionLabel:中的一个函数,将descriptionOfProgram的文本设置为等于该字符串

graphViewController出现在字符串中时,文本不在标签中。在测试过程中,我在GraphViewController中创建了一个名为test的按钮,以便在按下时调用setDescriptionLabel。这起到了作用。有什么想法吗?

我将GraphViewController.h.m包括在第二MVC中,将GraphingCalculatorViewController.m包括在主MVC中。

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。我去掉了所有与续集无关的额外功能。

图形计算器ViewController.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

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

最新更新