0

有很多類似的問題和答案,但它們並沒有解決主要問題:如何在其可操作的委託中正確添加cocos2d中的任何文本字段。在cocos2d中添加UITextFieldDelegate?

例如,如果我創建了UITextfield並將它添加到[[CCDirector sharedDirector]視圖],那麼它將只調用textFieldShouldReturn:方法。沒有更多的方法不工作,甚至textViewDidBeginEditing:

我給了使用單獨的視圖和UITextField相同的結果。我也試圖創建一個單獨的UIViewController利用其觀點與的UITextField,但它甚至讓我的應用程序倒下

回答

0

我的錯誤是UITextFieldDelegate和UITextViewDelegate有一些類似mehods,我用從第二委託一些方法。例如:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView; 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; 

當然,我在最後寫了(UITextView *)textField。

1

1:確保您CCNode包含UITextFieldDelegate

@interface MyLayer : CCLayer <UITextFieldDelegate> 

2:初始化給出UITextField按通常:

UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, fieldWidth, fieldHeight)]; 
      nameField.textAlignment = NSTextAlignmentCenter; 
      nameField.font = [UIFont fontWithName:kFontName size:24]; 
      nameField.text = @"Enter Name"; 
      nameField.delegate = self; 
      nameField.backgroundColor = [UIColor grayColor]; 
      nameField.textColor = [UIColor whiteColor]; 
      nameField.autocorrectionType = UITextAutocorrectionTypeNo; 

3:字段添加到CCDirector視圖:

[[[CCDirector sharedDirector] view] addSubview:nameField]; 

4:實現UITextField代表在CCNode

- (BOOL)textFieldShouldReturn:(UITextField*)textField 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
+0

謝謝。你沒有解決我的問題,但我發現我的錯誤 – user2083364