I am trying to develop a application by using Cocos2d. I can’t getting value from textfield . How can get the value of text field (in a alert view) by using 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]; }
Action sheets and alerts are handled asynchronously. In your case, the message [dialog show] merely schedules the show event for some later execution (handled by the main run loop). If you put in a few NSLog()s, you’ll see that the [show] message returns almost immediately, at which point your user hasn’t entered any data, nameField’s text is blank, and this converts to an int of 0.
You want a blocking, modal dialog with input — action sheets and alerts are not designed for user input beyond yes/no/cancel button pushes. You’ll have to cook up a view of your own: not too hard, but it will require more work than using an action sheet/alert.