textfieldshouldclear:UITextfield不调用

时间:2013-03-28 06:38:16

标签: ios uitextfield clear popover uitextfielddelegate

我试图在点击UITextField的清除按钮时调用-(BOOL) textFieldShouldClear:(UITextField *)textField。我已经设置了我的委托,并且正确调用了UITextField的其他委托方法,除了这个方法。清除按钮在nib文件中设置为“始终可见”。

修改

仅供参考我在更改文本字段的文本时显示FPPopover。如果我点击清除按钮而没有显示弹出窗口,清除按钮工作正常。但是如果我在显示弹出窗口时尝试点击它,则不会调用委托方法。

代码段

-(BOOL) textFieldShouldClear:(UITextField *)textField
{
    return YES;
}

- (IBAction)didChangeScripText:(id)sender {

    NSString *text = isPortrait ? symbolTextField.text : landsymbolTextfield.text;

    if(scripList.count == 0)
    {
        if([Logs sharedManager].scripData.count > 0)
            [self extractScrips];
        else
            return;
    }

    //        SAFE_ARC_RELEASE(popover);
//        popover=nil;

        //the controller we want to present as a popover
        if(controller == nil)
            controller = [[scripComboViewController alloc] initWithStyle:UITableViewStylePlain];

        if(controller.scripListFiltered.count > 0)
            [controller.scripListFiltered removeAllObjects];

        controller.delegate = self;
        if(popover == nil){
            popover = [[FPPopoverController alloc] initWithViewController:controller];
            popover.tint = FPPopoverDefaultTint;
        }

    controller.scripListFiltered = [NSMutableArray arrayWithArray:[scripList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",text]]];
    NSLog(@"array is: %@",controller.scripListFiltered);

    if(controller.scripListFiltered.count == 0)
    {
        [popover dismissPopoverAnimated:YES];
        return;
    }
        //decide contentsize and arrow dir based on tableview height

        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            popover.contentSize = CGSizeMake(300, 500);
        }
        else {
            popover.contentSize = CGSizeMake(200, 200);
        }

        //sender is the uitextfield
    float height = isPortrait ? portTable.frame.size.height : landTable.frame.size.height;
    if(height > 0)
        popover.arrowDirection = FPPopoverArrowDirectionDown;
    else
        popover.arrowDirection = FPPopoverArrowDirectionUp;

    if(![popover isModalInPopover])
        [popover presentPopoverFromView:sender];

    [controller reloadTable];

}

出了什么问题?有谁能告诉我。感谢。

2 个答案:

答案 0 :(得分:0)

实际上问题是由 FPPopover 引起的。当它在其视图之外接收触摸事件时,它会自行消除,并且当时不可能与外部控件进行交互。因此,如果点击清除按钮,它将用于解除弹出窗口,然后我可以使用清除按钮。多数民众赞成。

答案 1 :(得分:-1)

使用clearButtonMode的{​​{1}}属性。不需要使用UITextField方法。

textFieldShouldClear