I've been bashing my head against the walls for some time now and still I haven't managed to break this problem.

I have a modalViewController with on it a UITextField (let's call it myTroubleMaker) and several other non-important components. When I load the modalVC I init the UITextField and allow it to become the firstResponder right away, also I set it's delegate to the modalVC (self). The modalView loads nicely with the focus on the UITextField and the keyboard opening right away. But when I touch/click anywhere else on the screen it doesn't dismiss the keyboard. It does switch the focus off the UITextField and resigns firstResponder but the keyboard isn't dismissing.

What is causing this troublesome issue? How can I fix this properly?

Code:

.h

@interface myModalViewController : UIViewController <UItextFieldDelegate>
{
    UITextField *myTroubleMaker;
}

@property (nonatomic, retain) UITextField *myTroubleMaker;

@end

.m

//I synthesized it properly
-(void)loadView
{
    [super loadView];

    self.myTroubleMaker = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    self.myTroubleMaker.backgroundColor = [UIColor clearColor]; 
    self.myTroubleMaker.textColor = [UIColor whiteColor];
    self.myTroubleMaker.text = @"";
    self.myTroubleMaker.font = [UIFont systemFontOfSize:self.myTroubleMaker.frame.size.height - 8];
    self.myTroubleMaker.adjustsFontSizeToFitWidth = YES;
    self.myTroubleMaker.minimumFontSize = 0.0;
    self.myTroubleMaker.textAlignment = UITextAlignmentCenter;
    self.myTroubleMaker.autocorrectionType = UITextAutocorrectionTypeNo;
    self.myTroubleMaker.delegate = self;
    self.myTroubleMaker.placeholder = @"I eat ur brainz, yoh!";
    self.myTroubleMaker.keyboardType = UIKeyboardTypeDefault;
    self.myTroubleMaker.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:self.tabNameInputField];   
    [self.myTroubleMaker becomeFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.tabNameInputField.text = @"";
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{  
    UITouch *touch = [[event allTouches] anyObject];

    if ([touch view] == someOtherModalVCComponent) {
        //do stuff

        if([self.tabNameInputField isFirstResponder])
        {
            [self.tabNameInputField resignFirstResponder];
        }

        [self loadGraphics];
    }

    if ([touch view] != self.myTroubleMaker) 
    {
        if([self.myTroubleMaker isFirstResponder])
        {
            [self.myTroubleMaker resignFirstResponder];
        }
    }
}

It's all working the way I want except for this mindnumbing issue with the keyboard. Even as I wrote this question I checked similar problems for possible solutions.

Any help would be appreciated. I'm getting a headache here...


Solved by the solution I accepted below.

For future references I used devforums.apple.com/message/425914 to get my specific solution. Since I was passing the modalVC with a navigationController I needed to make a category on UINavigationController and add your method to it so it supported the automatic dismissal of the keyboard.

有帮助吗?

解决方案

You are probably presenting using UIModalPresentationFormSheet. If yes, then it is a feature on iPad that the keyboard won't be dismissed.

On iOS4 there is a new method disablesAutomaticKeyboardDismissal: that might help you if you override it to get rid of the default behavior.

http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/disablesAutomaticKeyboardDismissal

其他提示

One option is to implement a custom UIButton and send it to the back of your root view. on the button's click, resign your first responders.

There are more solutions like the other answer here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top