如何将TextField中的文本转换为字符串变量?我在.h文件中声明了textfield和variable,如下所示:
NewScene.h
cocos2d::ui::TextField* textField;
std::string enteredData;
然后在.cpp init()函数中声明了textField
,并且在按下菜单按钮后我想将写入的内容保存到变量中,所以我使用
enteredData=textField->getString();
但程序崩溃,给我一个访问冲突错误。谁能告诉我如何解决这个问题?
修改
这是代码:
bool NewScene::init()
...
auto textField = ui::TextField::create("Nick: ", "fonts/Marker Felt.ttf", 30);
textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
textField->setTextVerticalAlignment(TextVAlignment::CENTER);
textField->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
textField->setTouchAreaEnabled(true);
textField->setTouchSize(Size(200, 100));
textField->addEventListener(CC_CALLBACK_2(NewScene::textFieldEvent, this));
this->addChild(textField);
和功能:
void NewScene::textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type)
{
switch (type)
{
case ui::TextField::EventType::ATTACH_WITH_IME:
{
CCLOG("Clicked");
break;
}
case ui::TextField::EventType::DETACH_WITH_IME:
{
enteredData = textField->getString();
break;
}
}
我虽然在键入完成后会将文本保存到变量中,但我想它不会那样工作。