实现全局关闭所有键盘,actionSheet和alertView

在多页面的IOS程序中,当切换页面时,需要先将keyBoard,actionSheet或alertView这类模态化窗口隐藏。

以下代码分别实现全局关闭actionSheet和alertView,全局隐藏所有键盘。

关闭所有actionSheet和alertView:

- (void)closeModalView  
{  
    for (UIWindow* window in [UIApplication sharedApplication].windows)  
    {  
        for (UIView* view in window.subviews)  
        {  
            [self dismissActionSheetAndAletrtViewInView:view];  
        }  
    }  
}  
  
- (void)dismissActionSheetAndAletrtViewInView:(UIView*)view  
{  
    if ([view isKindOfClass:[UIActionSheet class]])  
    {  
        UIActionSheet *actionView = (UIActionSheet *)view;  
        [actionView dismissWithClickedButtonIndex:actionView.cancelButtonIndex animated:NO];  
    }  
    else if ([view isKindOfClass:[UIAlertView class]])  
    {  
        UIAlertView *alertView = (UIAlertView *)view;  
        [alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex animated:NO];  
    }  
    else  
    {  
        for (UIView* subView in view.subviews)  
        {  
            [self dismissActionSheetAndAletrtViewInView:subView];  
        }  
    }  
}
- (void)hideKeyBoard  
{  
    for (UIWindow* window in [UIApplication sharedApplication].windows)  
    {  
        for (UIView* view in window.subviews)  
        {  
            [self dismissAllKeyBoardInView:view];  
        }  
    }  
}  
  
-(BOOL) dismissAllKeyBoardInView:(UIView *)view  
{  
    if([view isFirstResponder])  
    {  
        [view resignFirstResponder];  
        return YES;  
    }  
    for(UIView *subView in view.subviews)  
    {  
        if([self dismissAllKeyBoardInView:subView])  
        {  
            return YES;  
        }  
    }  
    return NO;  
}

你可能感兴趣的:(实现全局关闭所有键盘,actionSheet和alertView)