美文网首页iOS开发iOS技能
UIActionSheet等使用注意

UIActionSheet等使用注意

作者: 李小争 | 来源:发表于2015-12-28 11:24 被阅读309次

使用Window的时候需要注意

公司代码中有一个HUD界面,创建的动画View是在Window上面的,使用[UIApplication sharedApplication].keyWindow
项目中有这样一个需求:

IMG_0181.jpg

点击支付的时候进行网络请求,在网络请求的时候需要显示HUD,但是HUD在其他页面显示正常,在这个页面就不能正常显示.

原因如下:

UIActionSheet、UIAlertView、GKPeerPickerController、UIAPopover、GKPanel 都是UIView的子类,如果直接在读取视图之上显示这些UIActionSheet等等的视图,是无法阻断事件的。因此系统自带的这些模态视图先被添加到一个UIWindow的视图,再把给window视图作为application的keyWindow显示。也就是在显示模态视图的时候,application的多window的。

当把HUD放在[UIApplication sharedApplication].keyWindow上面的时候,显然不是我们想要的那个window,


我的解决办法是获取所有的window,然后判断window的类型是不是UIWindow,如果是的话在执行.

UIWindow *window; // 肯定会有一个window的,所以这样写没有问题.
NSArray *windows = [UIApplication sharedApplication].windows;
for (UIWindow *win in windows) {
    if ([win isKindOfClass:[UIWindow class]]) {
        window = win;
    }
}

这样就不会把HUD创建在其他的window上面了.问题解决.

补充:
关于UIActionSheet中如果按钮点击事件是跳转到其他控制器的时候,最好在这里执行.

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED;  // after animation

例如:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self getAuthStatusRequest];
    }
}

相关文章

网友评论

    本文标题:UIActionSheet等使用注意

    本文链接:https://www.haomeiwen.com/subject/udibhttx.html