0

我正在尝试使用 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];

}
4

1 回答 1

0

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

您需要一个带有输入的阻塞模式对话框——操作表和警报不是为除了是/否/取消按钮按下之外的用户输入而设计的。您必须自己制作一个视图:不是太难,但它需要比使用操作表/警报更多的工作。

于 2009-03-31T15:18:55.933 回答