2

I have Used in cocos2d game engine. this is my code:

-(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 addButtonWithTitle:@"Done"];
    [dialog addButtonWithTitle:@"Cancel"];

    nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

        //timeStatus is a int type global variable //   
     timeStatus =[nameField.text intValue];  //** this line not working**//  

    [nameField release];
    [dialog show];

}
4

2 回答 2

2

您无法获得值的原因是该函数将在用户键入任何内容之前退出!当您调用 [dialog show] 时,对话框会显示给用户,然后他们可以在其中输入文本。

您应该等到用户选择一个按钮,这将触发您的委托功能,然后访问文本字段属性。

请注意,要执行此操作,您需要保留指向您添加的 UITextField 对象的指针(最简单),或者在子视图中再次找到它并进行转换。

于 2009-01-03T08:37:00.787 回答
0

Release nameField later, and get the text property from there.

于 2009-01-03T06:40:33.037 回答