1

下面的代码显示当用户在表格视图单元上做长按手势时,UIActionSheet会在其内部启动一个UITextField。当点击UITextField时,键盘启动,并且textFieldShouldBeginEditingtextFieldDidBeginEditing被调用,但文本字段不会接受关键点击。UIAextField在UIActionSheet中只调用一些委托方法

点击返回键将不会触发委托方法,但点击其中一个UIActionSheet按钮将触发textFieldShouldEndEditing,然后触发textFieldDidEndEditing

我设置textField成为第一响应者,所以我不知道为什么它不接受来自键盘的输入。有什么建议么?

- (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); 
     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; 
} 

回答

0

UITextFieldDelegateUIActionSheetDelegate在你的头?

如果没有,可能有问题,同时设置textfield.delegateself

1

你的问题是有点老,但作为回答没有标记,所以如果是你或其他观众有帮助我张贴我的解决方案from my own SO question。我开始时遇到了和你一样的问题,并最终发现UIActionSheet确实需要一些重要的事件才能使键盘正常工作。 不幸的是,我的链接发布的代码只能在纵向上使用,无论如何它都可以。