我正在尝试使用Cocos2d开发应用程序。我无法从textfield获得价值。如何使用Cocos2d获取文本字段的值(在警报视图中)?

-(void)timed1: (id)sender 
{
    UIAlertView* dialog = [[[UIAlertView alloc] init] retain];
    [dialog setDelegate:self];

    [dialog setTitle:@"Enter Time:"];
    [dialog setMessage:@" "];
    UITextField * nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [dialog addSubview:nameField];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0);
    [dialog setTransform: moveUp];
    [dialog setBackgroundColor:[UIColor clearColor]];
    [dialog addButtonWithTitle:@"Done"];
    [dialog show];

    nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    nameField.keyboardAppearance = UIKeyboardAppearanceAlert;
    nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;

        //  timeStatus is a int type global variable
    timeStatus =[nameField.text intValue]; // this line not working i can't getting value namefield


    [dialog release];
    [nameField release];

}
有帮助吗?

解决方案

操作表和警报是异步处理的。在您的情况下,消息[dialog show]仅调度show事件以供稍后执行(由主运行循环处理)。如果你输入几个NSLog(),你会看到[show]消息几乎立即返回,此时你的用户没有输入任何数据,nameField的文本是空白的,并且转换为int为0

您想要一个带输入的阻止模态对话框 - 操作表和警报不是为用户输入而设计的,超过是/否/取消按钮推送。你必须做一个自己的观点:不是太难,但需要比使用行动表/警报更多的工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top