2012-02-13 10 views
0

MYプログラムは簡単な電卓です。メインビューはCalcViewController.h/.mに接続されています。電卓の一番下には、サブビューのトップ100点がボタンで表示されています。ユーザが表示されているサブビューボタンをクリックすると、サブビューの残りの部分が、入力番号が表示されているラベルの最下部までスライドします。サブビュー(AdvancedView)には他のボタンが1つしかありません(sqrt)。これを押すと、現在ラベルにある数値を取得してモデルに送信して、平方根法を実行したいと考えています。私が抱えている唯一の問題は、UILabel IBOutlet *の表示が私のCalViewControllerに設定されていて、私のAdvancedViewではなく、それが必要なところです。アイブ氏は、私のAdvancedView.hにCalcViewController.hをインポートして、CalcViewControllerに*のMAINVIEWポインタを合成しようとしたが、表示部の数値があるかどう私は、私の詳細ビュー内から 他のコントローラからUILabel IBOutletのテキストにアクセスするにはどうすればよいですか?

double currentText = [self.mainView.display.text doubleValue]; 

を呼び出すとき、まだ0.000000を取得します。私はまた、ソリューションのために多くのネットを検索し、プロトコルを越えて来ていますが、私はこのコンセプトの周りを完全に覆い隠すことはできません。だから私の質問に戻る:UILabel IBOutlet *のテキストを別のコントローラクラスから表示するにはどうすればいいですか?

CalcViewController.h

#import <UIKit/UIKit.h> 
@interface CalcViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *display; 
@end 

CalcViewController.m

#import "CalcViewController.h" 
#import "CalculatorBrain.h" 
#import "AdvancedView.h" 

@interface CalcViewController() 
@property (nonatomic) BOOL userIsInTheMiddleOfTyping; 
@property (nonatomic, strong) CalculatorBrain *brain; 
@property (nonatomic, strong) AdvancedView *myNewView; 
@end 

@implementation CalcViewController 
@synthesize display = _display; 
@synthesize userIsInTheMiddleOfTyping = _userIsInTheMiddleOfTyping; 
@synthesize brain = _brain; 
@synthesize myNewView = _myNewView; 
- (AdvancedView *) myNewView 
{ 
    if (!_myNewView) _myNewView = [[AdvancedView alloc] initWithFrame:CGRectMake(0, 489, 320, 200)]; 
    return _myNewView; 
} 
- (void)setMyNewView:(AdvancedView *)myNewView 
{ 
    NSLog(@"what the duece"); 
} 
- (void)subView 
{ 
    int advancedTag = 199998; 
    self.myNewView.tag = advancedTag; 

    if (![self.view viewWithTag:advancedTag]) { 
     [self.view addSubview:self.myNewView]; 

    } 
} 
- (void)viewDidAppear:(BOOL)animated 
{ 
    // Show the top 100 of AdvancedView 
    [super viewDidAppear:animated]; 
    [self subView]; 

    CGRect newFrame = self.myNewView.frame; 
    newFrame.origin.y = 480-75; 
    self.myNewView.frame = newFrame; 
    NSString *testText = self.display.text; 
    NSLog(@"The test text is %@", testText); 

} 
- (CalculatorBrain *)brain 
{ 
    if (!_brain) _brain = [[CalculatorBrain alloc] init]; 
    return _brain; 
} 
- (IBAction)digitPressed:(UIButton *)sender { 
    NSString *digit = [sender currentTitle]; 
    if (self.userIsInTheMiddleOfTyping) 
    { 
     self.display.text = [self.display.text stringByAppendingString:digit]; 
    } else { 
     self.display.text = digit; 
     self.userIsInTheMiddleOfTyping = YES; 
    } 
} 
- (IBAction)operationPressed:(UIButton *)sender { 
    [self.brain pushOperandOne:[self.display.text doubleValue]]; 
    [self.brain pushOperator:sender.currentTitle]; 
    self.userIsInTheMiddleOfTyping = NO; 
} 
- (IBAction)enterPressed { 
    [self.brain pushOperandTwo:[self.display.text doubleValue]]; 
    // self.userIsInTheMiddleOfTyping = NO; 
    double result = [self.brain execute]; 
    NSString *resultString = [NSString stringWithFormat:@"%g", result]; 
    self.display.text = resultString; 

} 
- (IBAction)clear { 
    self.display.text = @"0"; 
    self.userIsInTheMiddleOfTyping = NO; 
    [self.brain cleadLastCalculatedValue]; 
} 
@end 

AdvancedView.h

#import <UIKit/UIKit.h> 

@interface AdvancedView : UIView 
@end 

AdvancedView.m

:ここでは私が働いているコードがあります
#import "AdvancedView.h" 
#import "CalculatorBrain.h" 
#import "CalcViewController.h" 

@interface AdvancedView() 
@property (nonatomic) BOOL toggleTheView; 
@property (nonatomic, strong) CalculatorBrain *subBrain; 
@property (nonatomic, strong) CalcViewController *mainView; 
@end 

@implementation AdvancedView 
@synthesize toggleTheView = _toggleTheView; 
@synthesize subBrain = _subBrain; 
@synthesize mainView = _mainView; 

- (CalcViewController *)mainView 
{ 
    if (!_mainView) _mainView = [[CalcViewController alloc] init]; 
    return _mainView; 
} 
- (CalculatorBrain *)subBrain 
{ 
    if (!_subBrain) _subBrain = [[CalculatorBrain alloc] init]; 
    return _subBrain; 
} 
- (id) initWithFrame:(CGRect)rect 
{ 
    if (self = [super initWithFrame:rect]) { 
     self.backgroundColor = [UIColor groupTableViewBackgroundColor]; 

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button addTarget:self 
        action:@selector(display:) 
     forControlEvents:UIControlEventTouchDown]; 
     [button setTitle:@"Advanced features" forState:UIControlStateNormal]; 
     button.frame = CGRectMake(80.0, 10.0, 160.0, 40.0); 
     [self addSubview:button]; 

     // sqrt button 
     self.backgroundColor = [UIColor groupTableViewBackgroundColor]; 

     UIButton *sqrt = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

     [sqrt addTarget:self 
        action:@selector(squareIt:) 
     forControlEvents:UIControlEventTouchDown]; 
     [sqrt setTitle:@"sqrt" forState:UIControlStateNormal]; 
     sqrt.frame = CGRectMake(80.0, 60.0, 160.0, 40.0); 

     [self addSubview:sqrt]; 



    } 
    return self; 
} 

- (void) squareIt:(id)sender 
{ 
    double currentText = [self.mainView.display.text doubleValue]; 
    NSLog(@"%f", currentText); 
    double result = [self.subBrain squareRoot:[self.mainView.display.text doubleValue]]; 
    NSLog(@"the text is: %f", result); 
    NSString *resultString = [NSString stringWithFormat:@"%g", result]; 
    self.mainView.display.text = resultString; 
} 

- (void) display:(id)sender 
{ 
    if (!_toggleTheView) { 

     float bottomYOfDigitToBeDisplayed = 75; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDelay:0.0]; 
     [UIView setAnimationDuration:0.5]; 
     self.frame = CGRectMake(0, bottomYOfDigitToBeDisplayed, 320, 400); 
     [UIView commitAnimations]; 
     self.toggleTheView = YES; 

    } else { 
     float bottomYOfDigitToBeDisplayed = 405; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDelay:0.0]; 
     [UIView setAnimationDuration:0.5]; 
     self.frame = CGRectMake(0, bottomYOfDigitToBeDisplayed, 320, 400); 
     [UIView commitAnimations]; 
     self.toggleTheView = NO; 
    } 

} 
@end 

注:モデルクラスの画面をもう使用したくないので、私はちょうどそれを取り除きました。それは単なる典型的なモデルです。あなたが今何をしているか

答えて

0

は簡単な言葉でそれを合計するAdvancedView

にCalcViewControllerの新しいビューを作っています。あなたがCalcViewControllerのビューを作っ

  1. ..
  2. が、その後AdvancedViewに新しいCalcViewController(_mainView)を作ると言うことができます はあなたが望むものをB
  3. はAのテキストの値であると言うことができます..しかし、あなたはあなたがする必要がどのよう

.. B..whose表示ラベルの値が値を持っていません取得しているCalcViewControllerシングルトン作る..です

は、アプリケーションライフタイムでCalcViewControllerの1つのオブジェクトのみを作成します。

CalcViewController.mでの#import文

static CalcViewController *view; 

その後、.hファイルで宣言されたメソッドを(追加前に追加します...

+ (CalcViewController *) getCalcViewController; 
{ 
    if (view == nil) 
    { 
     view = [[CalcViewController alloc] init]; 
    } 

    return view; 
} 

メートルで)定義されたことは今ちょうどあなたの答えの最初の部分は今完璧な理にかなって

double currentText = [[CalcViewController getCalcViewController].display.text doubleValue]; 
+0

を使用しています。しかし、静的なCalcViewController *ビューをインポートの上に追加しました。エラーが発生しました:unknown type name。私はその後、私のadvancedView.hにgetCalcViewControllerクラスメソッドを宣言し、エラーが発生しました:型が必要です。私がgetCalcViewControllerを.mで定義したとき、私はエラーが発生します:未定義の識別子 'view'の使用。限り、 'ビュー'エラーが私は私のAdvancedViewでそのプロパティを宣言する必要が推測して行く行く。これらの他のエラーはどうですか? – Scott

+0

okay .. CalcViewController.miのインポートステートメントの後に静的なCalcViewController *ビューを追加すると、ネイティブiOSデータタイプと混同されています..それを実行し、残りの手順に従います。 – Shubhank

+0

すべてのエラーを取り除きましたしかし、ラベルに9を入力すると、高度な機能ボタンを押してAdvancedViewを起動し、sqrtボタンを押します。0.000は現在のテキストに割り当てられています: – Scott

関連する問題