Cocos2dを使用してアラートビューでテキストフィールドの値を取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/415855

  •  03-07-2019
  •  | 
  •  

質問

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のテキストは空白で、これは0のintに変換されます。

ブロック付きのモーダルダイアログと入力が必要です。アクションシートとアラートは、ユーザーが[はい] / [いいえ] / [キャンセル]ボタンを押した場合以外は入力できません。独自のビューを作成する必要があります。それほど難しくはありませんが、アクションシート/アラートを使用するよりも多くの作業が必要になります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top