【问题标题】:UITextField in a UIActionSheet only calling some delegate methodsUIActionSheet 中的 UITextField 仅调用一些委托方法
【发布时间】:2011-02-10 04:49:39
【问题描述】:

下面的代码显示,当用户在表格视图单元格上执行长按手势时,UIActionSheet 会启动,其中包含UITextField。点击UITextField 时,键盘启动,并调用textFieldShouldBeginEditingtextFieldDidBeginEditing,但文本字段不接受按键。

点击返回键不会触发委托方法,但点击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;
}

【问题讨论】:

    标签: objective-c cocoa-touch uitableview uitextfield uiactionsheet


    【解决方案1】:

    你的问题有点老了,但没有标记为已回答,所以如果它对你或其他观众有帮助,我会发布我的解决方案from my own SO question。我一开始遇到了和你一样的问题,最后发现 UIActionSheet 真的吃掉了一些重要的事件,这些事件是让键盘正常工作所必需的。 不幸的是,我链接的发布代码仅适用于纵向,无论如何它确实如此。

    【讨论】:

      【解决方案2】:

      UITextFieldDelegateUIActionSheetDelegate 在您的标题中吗?

      如果不是,则将textfield.delegate 设置为self 时可能会出现问题

      【讨论】:

        猜你喜欢
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 2012-09-26
        • 2011-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多