0

我使用了来自 iPhone Developer's Cookbook 的名为 ModalAlert 的食谱,以便从用户那里获取一些文本;但是,当显示警报时,键盘和按钮会被冻结。这是模态警报的代码。

+(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

您可能应该检查 aUITextFielduserInteractionEnabled属性是否默认为 YES 或 NO。

于 2010-03-01T16:54:48.937 回答
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

在我的教育游戏 QPlus 中,我遇到了类似的问题。它困扰着我,因为我在两个相关应用程序中有“完全相同”的代码,而他们没有这个错误。原来,这个bug是因为头文件中没有声明选择器方法。我在 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 回答