2011-10-12 172 views
8

我試圖使用textFieldShouldBeginEditing來禁用鍵盤顯示自定義UITextField。我正在實施所有UITextFieldDelegate方法。但是,由於某種原因,textFieldShouldBeginEditing實際上永遠不會被調用。textFieldShouldBeginEditing not called

下面的委託方法總是叫:

– textFieldDidBeginEditing: 
– textFieldShouldEndEditing: 
– textFieldDidEndEditing: 

該視圖以下列方式結構:

UIViewController,其保持滾動視圖。根據視圖的狀態,此滾動視圖將包含UIView以及自定義UITextFields的列表。

我在這個設備上運行iOS 4.3.5(8L1)。

任何想法?

編輯;添加了一些代碼段:

UIViewController具有如下接口

@interface AViewController: UIViewController<UITextFieldDelegate> 

一旦UIViewController的負載,我所有UITextFields連接到使用

aSubView.aTextField.delegate = self; 

(簡體)代表位於AViewController

實現中,圖
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
} 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    return YES; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    return YES; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    return YES; 
} 

自定義UITextField代碼

簡體實現文件 -

#import "PVEntryTextField.h" 
#import "EntryViewController.h" 

@implementation PVEntryTextField 
@synthesize isPasswordField, state, singleTap; 

- (id)initWithCoder:(NSCoder *)inCoder 
{ 
    if (self = [super initWithCoder:inCoder]) 
    { 
     self.font = [UIFont fontWithName:@"Helvetica-Bold" size:19]; 
     self.textColor = [UIColor colorWithRed:51.0/255.0 
             green:51.0/255.0 
              blue:51.0/255.0 
             alpha:1.0]; 
     self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    } 

    return self; 
} 

- (CGRect)textRectForBounds:(CGRect)bounds 
{ 
    return CGRectMake(bounds.origin.x + 16, bounds.origin.y, 
         bounds.size.width - 16*2 - 10, bounds.size.height); 
} 

- (CGRect) editingRectForBounds:(CGRect)bounds 
{ 
    return [self textRectForBounds:bounds]; 
} 

- (BOOL) canBecomeFirstResponder 
{ 
    return YES; 
} 

- (void) updateState:(int) newState 
{ 
    state = newState; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 
+0

如果您的其他委託方法正在針對相同的文本字段進行調用,並且您的文本字段不是子類或任何內容,則只能來自方法名稱中的拼寫錯誤。 – jrturton

+0

我已經對UITextField進行了子類化,以對文本字段的外觀進行一些修改。但是,我實際上沒有觸及與代表有關的任何事情。 – Charles

+0

我不確定這個,但是,是否可以通過方法「canBecomeFirstResponder」的默認實現調用「textFieldShouldBeginEditing」?嘗試通過[super canBecomeFirstResponder]實現該方法或僅​​僅刪除它。 – lluismontero

回答

13

它是更多鈔票那textFieldShouldBeginEditing是由方法canBecomeFirstResponder的默認實現調用的?

嘗試執行[super canBecomeFirstResponder]的方法或只是刪除它。

+0

謝謝!我有一個'UITextField'子類,由於一些經驗性測試而覆蓋'canBecomeFirstResponder'。完全錯過了 – mkko

13

有你設置的UITextField授人以 「自我」?

+0

UITextField委託設置爲self。但是,這是從主UIViewController調用的。委託方法的實現也在主UIViewController中。 – Charles

+0

你必須設置UITextView委託給實現UITextViewDelegate方法的類 – lluismontero

+0

是的,這就是我所做的。 :) – Charles

0

不知道是否是一個錯字錯誤在這裏複製的代碼,但該方法名是不正確的:

正確 - textFieldShouldBeginEditing

YOURS - textFieldShoulBeginEditing(失蹤 「d」)

+0

複製錯誤。 ( – Charles

+2

,這將只會導致編譯錯誤... –

2

對於任何人來到這裏,並沒有找到解決辦法。我的問題是我在IB中創建了textField,然後在我的viewDidLoad中分配了一個。當我刪除實例化時,代理正確地工作,因爲它與正確的TF相關聯。

//I REMOVED these two lines because I created the textfield in IB 
_nameTextField = [[UITextField alloc] init]; 
_priceTextField = [[UITextField alloc] init]; 


[_nameTextField setDelegate:self]; 
[_priceTextField setDelegate:self]; 
2

我也遇到了沒有調用textFieldShouldEndEditing或textFieldShouldReturn的問題。這發生在更新我的應用程序的Storyboard之後。

當UITextFields位於ViewController子類的一部分時,委託方法被調用。

但是,當它們被移動到ViewController中的滾動視圖中時,方法不再被調用。

在ViewDidLoad中將其應用程序委託設置爲self可解決此問題。

self.emailField.delegate = self; 
self.passwordField.delegate = self; 
+0

令人驚歎。在我的情況下,我有一種觀點在導航控制器中,沒有任何工作正確......觸摸事件,文本框等。我按照你的建議分配了代表,並且它工作......完全廢話。 –

0

在Swift-3中需要在textField之前的「_」以便這個委託方法將會調用。在我的情況下,它可以幫助我。

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 
}