2013-11-04 22 views
0

我正在尋找的UITextField的方法,這讓我以後在爲UITextField鍵入一個東西類型。方法的UITextField - 之後我在爲UITextField

我不得不進行計算我用一個按鈕的方法,但是我不想用,我希望你以後我的UITextField中鍵入數字運行此方法。

任何提示哪種方法使用?

感謝您的幫助。

回答

3
  1. 貫徹UITextFieldFieldDelegate協議
  2. 設置你TextField的委託到視圖控制器

self.textField.delegate = self;

然後實現shouldChangeCharactersInRange方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
    { 
     textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; 
     [self doCalculations]; 
     return NO; 
    } 
+0

你好,凱文, 這種方法你花了或多或少的工作。 當我在計算中輸入一個值爲TextField時,後退爲一個字符。 文本字段= 11 退件標籤:111 –

+0

更新的答案(在我的部分愚蠢的錯誤) – Kevin

1

您可以使用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string得到鍵入的文本,立竿見影。或者,您可以使用- (BOOL)textFieldShouldReturn:(UITextField *)textField來捕獲用戶何時返回並僅在當時從文本字段中讀取文本。這兩種方法都在UITextFieldDelegate協議中,文檔可以在here找到。

編輯:

另外,您可以使用[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];貫徹框TextChanged方法來捕獲EditingChanged事件。

+0

感謝您將編輯標籤。這正是我所期待的。 –

0
//in your ViewContoller.h 
//Implement UITextFieldFieldDelegate protocol 


//In your ViewContoller.m 
//where you are creating your textfield. 
textField.delegate = self; 

// There are 2 delegate method that you can implement 
// this method will be called whwn ever you enter a a letter. 
// say you enter 'abcd' then this method will be called 4 times. 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
//do your calculation if required 
    return YES; 
} 

//this method will be called when the textField will end editing, i.e when the keyboard resigns of the control goes to some other textField/textView 
- (void)textFieldDidEndEditing:(UITextField *)textField { 

} 

根據您的要求,您可以使用第1種或第2種方法進行計算。

0

是的,你可以通過使用此代碼這樣做: -

此代碼詢問,如果委託指定的文本應該改變。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
    { 
     [self yourMethodName]; //The method which you want to call. 
     return YES; 
    } 

,並調用UITextFieldField Delegate.For更好地理解這一點,你可以參考這個: -

https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString

希望這將幫助你。