将委托设置为UISearchBar的文本字段时出现`unrecognized selector`错误

时间:2015-06-02 02:54:14

标签: ios objective-c uitextfield uisearchbar unrecognized-selector

我正在使用iOS8.0,我想在UISearchBar控件中将UITextFieldDelegate设置为UITextField。 我已成功在搜索栏中获取文本字段,我也可以为其设置自定义文本。 但是当我将UITextFieldDelegate设置为该TextField时,只要我在搜索栏内触摸,应用程序就会崩溃。

如果我有任何错误,请帮忙看看并告诉我。谢谢。 这是崩溃日志:

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [LoginToSafraViewController   _searchController]:无法识别的选择器发送到实例0x7b469440'   ***第一次抛出调用堆栈:(0 CoreFoundation 0x00962746 exceptionPreprocess + 182 1 libobjc.A.dylib
  0x005eba97 objc_exception_throw + 44 2 CoreFoundation
  0x0096a705 - [NSObject(NSObject)doesNotRecognizeSelector:] + 277 3
  CoreFoundation 0x008b1287 ___转发_
+ 1047     4 CoreFoundation 0x008b0e4e   _CF_forwarding_prep_0 + 14 5 UIKit 0x012b0e2b - [UISearchBarTextField canBecomeFirstResponder] + 96 6
  UIKit 0x010fabac - [UIResponder(内部)   _canBecomeFirstResponder] + 33 7 UIKit 0x010f923a - [UIResponder becomeFirstResponder] + 240 8 UIKit
  0x00fb8348 - [UIView(Hierarchy)becomeFirstResponder] + 114 9 UIKit   0x017763e7 - [UITextField becomeFirstResponder] + 51 10 UIKit
  0x0137f7f8    - [UITextInteractionAssistant(UITextInteractionAssistant_Internal)setFirstResponderIfNecessary] + 200 11 UIKit
  0x01381e26    - [UITextInteractionAssistant(UITextInteractionAssistant_Internal)oneFingerTap:] + 2762 12 UIKit
  0x01375677 _UIGestureRecognizerSendActions + 327 13 UIKit
  0x01373ef4 - [UIGestureRecognizer _updateGestureWithEvent:buttonEvent:]   + 561 14 UIKit 0x01375f3d - [UIGestureRecognizer _delayedUpdateGesture] + 60 15 UIKit 0x013798ba ___UIGestureRecognizerUpdate_block_invoke661 + 57 16   UIKit 0x0137977d

查找文本字段的代码:

UITextField *searchTextField = [self findTextFieldInsideSearchBar:x];
if (searchTextField != nil)
{
    [arr addObject:searchTextField];
    [searchTextField setText:@"sfsfsdf"];
    [searchTextField setDelegate:self];
}


-(UITextField*) findTextFieldInsideSearchBar:(UISearchBar*)searchBar
{
    for (UIView* view in searchBar.subviews) {
        if([[UIDevice currentDevice] systemVersion].floatValue >= 7){
            //at iOS v7.0 and later,
            for (UIView* subSubView in view.subviews) {
                if([subSubView isKindOfClass:[UITextField class]])

                    return (UITextField*) subSubView;
            }
        }else{
            //iOS v6.0 and erlier.
            if([view isKindOfClass:[UITextField class]])
                return (UITextField*) view;
        }
    }
    return nil;
}

1 个答案:

答案 0 :(得分:0)

您无法直接设置此文本字段的委托;

你应该继承你的searchBar并像这样设置textfield委托;

MySearchBar* mySearchBar = [[MySearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
mySearchBar.textFieldDelegate = self; // TO BE NOTED. SETTING textFieldDelegate. SAME WOULD WORK FOR XIB OR STORYBOARD
[self.view addSubview:mySearchBar];

========================

//  MySearchBar.h

#import <UIKit/UIKit.h>

@interface MySearchBar : UISearchBar

@property (nonatomic, weak) id<UITextFieldDelegate> textFieldDelegate;

@end

===============

//  MySearchBar.m

#import "MySearchBar.h"

@implementation MySearchBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textFieldShouldBeginEditing:)])
    {
        return  [self.textFieldDelegate textFieldShouldBeginEditing:textField];;
    }
    else
    {
        return true;
    }
} //F.E.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
    {
        [self.textFieldDelegate textFieldDidBeginEditing:textField];;
    }
}//F.E.

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        return  [self.textFieldDelegate textFieldShouldEndEditing:textField];;
    }
    else
    {
        return true;
    }
} //F.E.

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textFieldDidEndEditing:)])
    {
        [self.textFieldDelegate textFieldDidEndEditing:textField];;
    }
} //F.E.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)])
    {
        return  [self.textFieldDelegate textField:textField shouldChangeCharactersInRange:range replacementString:string];;
    }
    else
    {
        return true;
    }
} //F.E.

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textFieldShouldClear:)])
    {
        return  [self.textFieldDelegate textFieldShouldClear:textField];;
    }
    else
    {
        return true;
    }
} //F.E.

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.textFieldDelegate && [self.textFieldDelegate respondsToSelector:@selector(textFieldShouldReturn:)])
    {
        return  [self.textFieldDelegate textFieldShouldReturn:textField];;
    }
    else
    {
        return true;
    }
} //F.E.

@end