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