[IOS Code]实现全局关闭所有键盘,actionSheet和alertView

来源:互联网 发布:网络加持是什么意思 编辑:程序博客网 时间:2024/05/17 11:35

在多页面的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;}



原创粉丝点击