2012-03-02 30 views
1

當第二個ViewController(GraphViewController)UILabel(descriptionOfProgram)需要在原始MVC的segue發生時設置。iOS UILabel在使用segue時未更新的新視圖控制器

目前,我的主要MVC,我做兩件事情在我的prepareForSegue:

  1. 我設置GraphVewController的@property NSString到我希望它是字符串。
  2. 我通過調用GraphViewController setDescriptionLabel:中的函數將descriptionOfProgram的文本設置爲等於該字符串。

graphViewController出現在字符串中時,文本不在標籤中。在測試期間,我在GraphViewController中創建了一個名爲test的按鈕,在按下時調用setDescriptionLabel。這工作。關於發生什麼事的任何想法?

I在第二個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

prepareForSegue:viewDidLoad:之前被稱爲所以你會IBOutlet中是零。嘗試將傳入值設置爲NSString,然後使用該字符串填充標籤viewDidLoad:

+0

謝謝!我正在觀看斯坦福大學講座系列,我還沒有看到viewDidLoad功能: -/ 非常感謝。 – gg67 2012-03-02 17:21:51

相關問題