找出界面的第一响应者,让键盘消失

来源:互联网 发布:java怎么发送邮件 编辑:程序博客网 时间:2024/05/16 12:43

//方法1

 

 

    NSArray *wsArray = [[UIApplication sharedApplication] windows];

    for(UIView *aView in wsArray){

        [self resignKeyBoardInView:aView];

    }

- (void)resignKeyBoardInView:(UIView *)view 

    for (UIView *aView in view.subviews) { 

        if ([aView.subviews count] > 0) { 

            [self resignKeyBoardInView:aView]; 

        } 

//当只有UITextView,UITextField情况会影响键盘时,加条件判断

//如果是系统短信界面的键盘则不要判断条件,直接执行 [aView resignFirstResponder]; 

        if ([aView isKindOfClass:[UITextView class]] || [aView isKindOfClass:[UITextField class]] ) { 

            [aView resignFirstResponder]; 

        } 

    } 


//方法2

-(void)findFirstResponder

{

    UIWindow *tempWindow = nil;

    int viewCount = 0;

    pTextView = nil;

    int count = [[[UIApplication sharedApplication] windows] count];

    

    for (int k = 0; k<count; k++) {

        tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:k];

        viewCount = [tempWindow.subviews count];

        UIView *tempView = nil;

        for (int index = 0; index<viewCount; index++) {

            tempView = [tempWindow.subviews objectAtIndex:index];

            if ([self findAndResignFirstResponder:tempView]) {

                break;

            }

        }

        if (pTextView) {

            break;

        }

    }

    [pTextView resignFirstResponder];//找出此时第一响应者pTextView,执行键盘消失


}

-(BOOL)findAndResignFirstResponder:(UIView *)aView

{

    

    if (aView.isFirstResponder) {

        if ([aView isKindOfClass:[UITextView class]]) {

            pTextView = (UITextView *)aView;

            

        }

        else if([aView isKindOfClass:[UITextField class]])

        {

         pTextView = (UITextView *)aView;

        

        }

        else {

            pTextView = nil;

        }

        return YES;

        

    }

    for (UIView *subView in aView.subviews) {

        if ([self findAndResignFirstResponder:subView]) {

            return YES;

        }

    }

    return NO;

   

}


0 0
原创粉丝点击