开发者

UITextField in a UIActionSheet only calling some delegate methods

开发者 https://www.devze.com 2023-02-09 09:54 出处:网络
The below code shows that when a user does a long press gesture on a Table View Cell, then a UIActionSheet launches with a UITextField inside of it. When tapping the UITextField, the keyboard launches

The below code shows that when a user does a long press gesture on a Table View Cell, then a UIActionSheet launches with a UITextField inside of it. When tapping the UITextField, the keyboard launches, and textFieldShouldBeginEditing and textFieldDidBeginEditing get called, but the text field won't accept the key taps.

Hitting the return key won't trigger the delegate methods, but tapping one of the UIActionSheet buttons will trigger textFieldShouldEndEditing and then textFieldDidEndEditing.

I'm setting the textField to become the first responder, so I'm not sure why it's not accepting input from the keyboard. Any suggestions?

- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
    // only when gesture was recognized, not when ended
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // get affected cell
        SinTableViewCell *cell = (SinTableViewCell *)[gesture view];

        // get indexPath of cell
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        // do something with this action
        NSLog(@"Long-pressed cell at row %d", indexPath);
开发者_JAVA百科        AppDelegate_Shared *appDelegate = (AppDelegate_Shared*)[UIApplication sharedApplication].delegate;

        //setup UITextField for the UIActionSheet
        UITextField *textField    = [[UITextField alloc] initWithFrame:CGRectMake(0, 170, 320, 200)];
        textField.borderStyle     = UITextBorderStyleBezel;
        textField.backgroundColor = UIColorFromRGB(0XFFFFFF);
        textField.text            = @"";
        textField.delegate        = self;

        [textField setKeyboardType:UIKeyboardTypeAlphabet];
        [textField setKeyboardAppearance:UIKeyboardAppearanceAlert];

        //setup UIActionSheet
        UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Add Notes" 
                                                            delegate:self 
                                                   cancelButtonTitle:@"Cancel" 
                                              destructiveButtonTitle:nil 
                                                   otherButtonTitles: @"Save", nil];

        [asheet showFromTabBar:appDelegate.tabBarController.tabBar];  
        [asheet setFrame:CGRectMake(0, 100, 320,380)]; 
        [asheet insertSubview:textField atIndex:0];
        //[textField becomeFirstResponder];
        //memory management
        [textField release];
        [asheet release];
    }
}

#pragma mark -
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {

}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

}

#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldShouldBeginEditing");
    return YES;
}     
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldDidBeginEditing");
    [textField becomeFirstResponder];
}   
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"textFieldShouldEndEditing");
    return YES;
}

//should save the notes value here, I think
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"textFieldDidEndEditing");
    [textField resignFirstResponder];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    NSLog(@"textFieldShouldClearEditing");
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"in textFieldShouldReturn");
    return YES;
}


Your question is a little bit old but not marked as answered, so if it is helpful for you or other viewers I post my solution from my own SO question. I started up with the same problem as you had and ended up in the fact that UIActionSheet really eats up some important events which are necessary to get the keyboard working properly. My linked posted code unfortunately works only in portrait orientation, anyway it does it.


Is UITextFieldDelegate and UIActionSheetDelegate in your header?

If not, there could be problems while setting the textfield.delegate to self

0

精彩评论

暂无评论...
验证码 换一张
取 消