2014-01-15 81 views
0

我正在做一個應用程序在Xcode。它打算做一個計算,它將滑塊值除以100,再乘以用戶確定的文本字段(numberfield1)值。假設結果是在另一個文本字段(numberfield2)中顯示。但是,該值不會顯示在第二個文本字段中。我的問題是我需要在我的代碼中更正以在我的文本字段中顯示計算結果?計算值不會顯示在文本字段

這裏是我的.h文件:

@property (weak, nonatomic) IBOutlet UISlider *slider; 
@property (weak, nonatomic) IBOutlet UILabel *counter; 
@property (weak, nonatomic) IBOutlet UILabel *label; 
@property (weak, nonatomic) IBOutlet UITextField *numberfield1; 
@property (weak, nonatomic) IBOutlet UITextField *numberfield2; 

- (IBAction)changeLabel; 
- (IBAction)calculate:(id)sender; 

繼承人是我的.m文件:

- (IBAction)changeLabel {_counter.text = [[NSString alloc] initWithFormat:@"%1.f"  
,_slider.value];} 


- (IBAction)calculate:(id)sender { 
float x = [_counter.text floatValue]; 
float y = 100; 
float calc_result = x/y; 
float numberField1Float = [_numberfield1.text floatValue]; //converts the string in the text field to a float 
float calc_result2 = calc_result * numberField1Float; 
self.numberfield2.text = [NSString stringWithFormat:@"%0.1f", calc_result2]; 
} 
+0

1日檢查您的textfld連接界面生成器或不? –

+0

你確定IBActions能夠正確啓動嗎?你是否從界面生成器連接到IBAction? – heximal

回答

0

試試這個:

[self.numberfield2 setStringValue: 
    [NSString stringWithFormat: @"%0.1f", calc_result2]];