0

ユーザーからテキストを取得するために、ModalAlert という iPhone Developer's Cookbook のレシピを使用しました。ただし、アラートが表示されると、キーボードとボタンがフリーズします。モーダル アラートのコードを次に示します。

+(NSString *) textQueryWith: (NSString *)question prompt: (NSString *)prompt button1: (NSString *)button1 button2:(NSString *) button2
{
    // Create alert
    CFRunLoopRef currentLoop = CFRunLoopGetCurrent();
    ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:@"\n" delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil];

    // Build text field
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 30.0f)];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    tf.tag = TEXT_FIELD_TAG;
    tf.placeholder = prompt;
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.keyboardType = UIKeyboardTypeAlphabet;
    tf.keyboardAppearance = UIKeyboardAppearanceAlert;
    tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
    tf.autocorrectionType = UITextAutocorrectionTypeNo;
    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    // Show alert and wait for it to finish displaying
    [alertView show];
    while (CGRectEqualToRect(alertView.bounds, CGRectZero));

    // Find the center for the text field and add it
    CGRect bounds = alertView.bounds;
    tf.center = CGPointMake(bounds.size.width / 2.0f, bounds.size.height / 2.0f - 10.0f);
    [alertView addSubview:tf];
    [tf release];

    // Set the field to first responder and move it into place
    [madelegate performSelector:@selector(moveAlert:) withObject:alertView afterDelay: 0.7f];

    // Start the run loop
    CFRunLoopRun();

    // Retrieve the user choices
    NSUInteger index = madelegate.index;
    NSString *answer = [[madelegate.text copy] autorelease];
    if (index == 0) answer = nil; // assumes cancel in position 0

    [alertView release];
    [madelegate release];
    return answer;
}

ありがとう!

4

3 に答える 3

0
// Put the modal alert inside a new thread.  This happened to me before, and this is how i fixed it.

- (void)SomeMethod {
[NSThread detachNewThreadSelector:@selector(CheckCurrentPuzzle) toTarget:self withObject:nil]; }

-(void) CheckCurrentPuzzle {

NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];

// code that should be run in the new thread goes here


if ([gameBoard AreAllCellsFilled]) {

    if ([gameBoard FilledWithoutWin]) {

        //only show this message once per puzzle
        if (![currentPuzzle showedRemovalMessage]) {

            NSArray *buttons = [NSArray arrayWithObject:@"Yes"];

            if ([ModalAlert ask:@"blah blah blah" withTitle:@"Incomplete Puzzle" withCancel:@"No" withButtons:buttons] == 1) {
                NSLog(@"Remove The Incorrect Cells");

                [gameBoard RemoveIncorrect];

            } else {
                [gameSounds.bloop2 play];
            }               
        }

    } else {

        if ([gameBoard IsBoardComplete]) {
            [self performSelectorOnMainThread:@selector(WINNER) withObject:nil waitUntilDone:false]; 
        }
    }

}

    [pool2 release]; 
}

-(void) WINNER {

    //ladies and gentleman we have a winner

}
于 2010-12-07T23:13:58.217 に答える
0

おそらく、 aUITextFielduserInteractionEnabledプロパティのデフォルトが YES か NO かを確認する必要があります。

于 2010-03-01T16:54:48.937 に答える
0

私の教育用ゲーム QPlus で、これと同様の問題が発生しました。関連する 2 つのアプリに「まったく」同じコードがあり、それらにはバグがなかったため、バグが発生しました。ヘッダー ファイルでセレクター メソッドが宣言されていないことが原因であることが判明しました。私はXcode 4.2で作業しています。

以下の詳細:

.m:

tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(emailLabelPressed)];
tapRecognizer.numberOfTapsRequired = 1;
[aLabel addGestureRecognizer:tapRecognizer];
[aLabel setUserInteractionEnabled:YES];

そして後で .m:

  • (void)emailLabelPressed { //詳細 }

シミュレーターでは問題なく動作しますが、実際のデバイスでは、モーダルに表示される電子メール インターフェイスは編集されません。下書きとして送信または保存できますが、編集はできません。

次に、これを .h ファイルに追加します。

  • (void)emailLabelPressed;

そしてほら、それはデバイス上で動作します。もちろん、これは関連するアプリとの違いです。どちらもヘッダー ファイルでメソッドが宣言されていました。私はこれを iOS のバグとして分類しますが、初心者の開発者である私にはわからないでしょう。

これに基づいて、セレクター メソッド moveAlert: がヘッダー ファイルで宣言されていることを確認することができます。

楽しみ、

ダミアン

于 2011-11-23T06:37:30.147 に答える