2012-10-22 17 views
2

我仍在使用我的轉換器應用程序,並且我再次偶然發現了一個問題。連接UITextField以更新標籤

雖然試圖在文本字段中進行所有編輯以觸發計算代碼,但不會發生任何事情。這就是大氣壓發生:

trying to enter numbers

計算將不會觸發,直到我更改了撿拾器的東西。我不是在指責代碼,因爲我是這麼做的。

在試圖解決這個問題時,我從一個好友那裏得到了一些幫助,並且添加了下面的代碼來使它工作。

首先,

- (void)textFieldChanged:(UITextField *)textField 
{ 
[self updateConversionLabel]; 
} 
中的.m的beggining

,並在.H以下(我知道它的兩個,並且那可能是錯誤的,但我想嘗試這兩種)。

-(IBAction)textFieldChanged:(UITextField *)textField; 
-(void)textFieldChanged:(UITextField *)textField; 

計算如下:

#pragma mark - 
#pragma mark PickerView Delegate 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
[self updateConversionLabel]; 
} 

- (void)updateConversionLabel 
{ 
float convertFrom = [[_convertRates objectAtIndex:[picker selectedRowInComponent:0]] floatValue]; 

float convertTo = [[_convertRates objectAtIndex:[picker selectedRowInComponent:1]] floatValue]; 

float input = [inputText.text floatValue]; 
float to = convertTo; 
float from = convertFrom; 

float convertValue = input; 
float relative = to/from; 
float result = relative * convertValue; 

NSString *convertFromName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:0]]; 
NSString *convertToName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:1]]; 



NSString *resultString = [[NSString alloc]initWithFormat: 
          @" %.4f %@",result, convertToName]; 
resultLabel.text = resultString; 

NSString *formelString = [[NSString alloc]initWithFormat: 
          @" %.4f %@=", convertValue, convertFromName]; 
formelLabel.text = formelString; 


} 

我估計誤差可能在界面生成器,所以這裏是連接s connections inspector

有沒有簡單的解決方案呢? :)

+0

如果您在設置label.text之前添加日誌,那麼該值是您的控制檯中輸出的日誌值? – propstm

+0

如果你在這裏,它沒有輸出。 NSString * resultString = [[NSString alloc] initWithFormat: @「%.4f%@」,result,convertToName]; NSLog(@「Test」); resultLabel.text = resultString; –

+0

@propstm這是一個壞跡象嗎? :P –

回答

3

而不是使用「值更改」操作,使用「編輯已更改」作爲文本字段。

1

確保您的視圖控制器符合UITextFieldDelegate。將viewController設置爲界面構建器中的UITextField的委託。然後在你的viewController實現這個委託方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 

該委託方法獲取每次更改文本字段時間,以便觸發在那裏你的計算解僱。

+0

我很抱歉,這對我無能爲力。你能否請進一步解釋?我應該在哪裏放這個代碼?我沒有文本字段的代表,我需要這個嗎?我認爲它並不是那麼複雜,只是在問題 –

0

將textFieldChanged操作更改爲'編輯已更改'並將所有計算代碼放入textFieldChanged操作方法中。

+0

soz mate中的代碼的快速修復,你是2遲4天免費代表 –