iOS Ipad UIAlertController 崩溃解决方法

兼容ipad的 UIAlertController


-(void)showSheet:(SYUser *)user IndexPath:(NSIndexPath *)indexPath{
    
    SYUser *loginUser=[SYUser sharedLoginUser];
    if (loginUser.userRole !=UserRoleEosAdmin) {
        return;
    }
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle: UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:WYLocalizedString(@"co_cancel", @"") style:UIAlertActionStyleCancel handler:nil];
    
    WS(ws)
    UIAlertAction *archiveAction=[UIAlertAction actionWithTitle:WYLocalizedString(@"co_delete", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [ws userDelete:user];
    }];
    
    NSString *frozenTitle=@"";
    if (user.status==2) {
        frozenTitle=[NSString stringWithFormat:@"%@",WYLocalizedString(@"co_frozen", @"")];
    }else{
        frozenTitle=[NSString stringWithFormat:@"%@",WYLocalizedString(@"co_unfreeze", @"")];
    }
    
    UIAlertAction *frozenAction = [UIAlertAction actionWithTitle:frozenTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [ws doFrozen:user];
    }];
    
    [alertController addAction:frozenAction];
    if (user.status==1) {
        [alertController addAction:archiveAction];
    }
    [alertController addAction:cancelAction];
    //核心代码. 兼容ipad
    if ([SYAboutDevice currentDeviceType] == UIUserInterfaceIdiomPad) {
        
        CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath];
        CGRect rectInSuperView = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
        
        UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
        popPresenter.sourceView =[self.tableView superview];
        popPresenter.sourceRect =rectInSuperView;
        popPresenter.permittedArrowDirections = UIPopoverArrowDirectionUp;
        [self presentViewController:alertController animated:YES completion:nil];
    }else{
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

判断设备是ipad 还是iphone


+(UIUserInterfaceIdiom)currentDeviceType{
    if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad){
        return UIUserInterfaceIdiomPad;
    }else{
        return UIUserInterfaceIdiomPhone;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容