cocos2D UITextFieldの文字の取得について
Posted: 2013年1月03日(木) 15:41
こんにちは
いつも楽しく拝見させて頂いております。
私は現在cocos2Dでゲームを制作しているのですがレイヤーにUITextFieldを貼付け、そこに入力された文字列を
同じレイヤーに貼付けたラベルに表示させるという作業をしておりますが、どうもテキストフィールドに入力された文字の取得に
失敗しているらしくラベルには(null)が表示されます。
UITextFieldはCCUIViewWrapperを使っています
ラベルはCCLabelTTFです
ご教授お願いいたします。
h
m
いつも楽しく拝見させて頂いております。
私は現在cocos2Dでゲームを制作しているのですがレイヤーにUITextFieldを貼付け、そこに入力された文字列を
同じレイヤーに貼付けたラベルに表示させるという作業をしておりますが、どうもテキストフィールドに入力された文字の取得に
失敗しているらしくラベルには(null)が表示されます。
UITextFieldはCCUIViewWrapperを使っています
ラベルはCCLabelTTFです
ご教授お願いいたします。
h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCUIViewWrapper.h"
@interface charaMaking : CCLayer <UITextFieldDelegate>
{
UITextField *namefield;
CCUIViewWrapper *textFieldWrapper;
NSString *namae_kakutei;
}
@property (nonatomic ,copy)NSString *charaname;
@property (nonatomic ,retain)CCLabelTTF *label;
@end
m
#import "charaMaking.h"
@implementation charaMaking
@synthesize charaname;
@synthesize label;
-(id)scene {
CCScene *scene = [CCScene node];
CCLayer *backgroundlayer = [CCLayer node];
[scene addChild:backgroundlayer];
return scene;
}
-(void)addTextfield
{
namefield = [[[UITextField alloc] init] autorelease];
namefield.frame = CGRectMake(175, 120, 110, 25);
namefield.borderStyle = UITextBorderStyleRoundedRect;
namefield.placeholder = @"6文字以内!";
namefield.returnKeyType = UIReturnKeyDone;
namefield.clearButtonMode = UITextFieldViewModeWhileEditing;
namefield.autocapitalizationType = UITextAutocapitalizationTypeNone;
namefield.enablesReturnKeyAutomatically = YES;
namefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
namefield.textAlignment = NSTextAlignmentCenter;
textFieldWrapper = [CCUIViewWrapper wrapperForUIView:namefield];
namefield.delegate = self;
[self addChild:textFieldWrapper];
}
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)inputText //文字数6文字制限
{
BOOL result;
NSUInteger maxLength = 6;
if([inputText compare:@"\n"]==0) {
result=YES;
}
else {
NSUInteger textLength = namefield.text.length;
NSUInteger rangeLength = range.length;
NSUInteger stringLength = inputText.length;
NSUInteger length = textLength - rangeLength + stringLength;
result = (length <= maxLength);
}
return result;
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField { //Doneでキーボードを閉じる
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField { //namae_kakuteiにテキストフィールドの文字列を代入
namae_kakutei = namefield.text;
}
-(id) init {
if ((self = [super init])) {
[self addTextfield]; //UITextField
charaname = [NSString stringWithFormat:@"%@",namae_kakutei];
self.label = [CCLabelTTF labelWithString:charaname fontName:@"Times New Roman" fontSize:20];
self.label.position = CGPointMake(100, 100);
self.label.color = ccc3(0,0,0);
[self addChild:self.label z:1];
}
return self;
}
-(void) onExit {
[super onExit];
}
-(void) dealloc {
[charaMaking release];
[super dealloc];
}
- (void)onEnd:(ccTime)dt
{
[[CCDirector sharedDirector] replaceScene:[Title node]];
}
@end