在iPhone和iPad上清除视图控制器解雇

问题描述 投票:1回答:1

我有一个用于编辑对象细节的UIViewController子类,它被推送到iPhone上的UINavigationController

在iPad上,它是UINavigationController中的根视图控制器,带有presentationStyleUIModalPresentationPopover。换句话说,该演示文稿是推动iPhone和iPad上的模态/弹出窗口。

所以演示文稿在代码中完成如下:

苹果手机:

self.navigationController.pushViewController(detailVC, animated: true)

iPad的:


let nc = UINavigationController(rootViewController: detailVC)
nc.modalPresentationStyle = .popover
self.present(nc, animated: true)

我正在重新审视解除此视图控制器的代码;它需要弹出iPhone上的导航堆栈或关闭iPad上的弹出窗口。

目前代码如下:

        if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
            self.navigationController?.popViewController(animated: true)
        } else {
            self.presentingViewController?.dismiss(animated: true, completion: nil)
        }

但令我印象深刻的是,几乎可以肯定是一个更清晰的解决方案 - 可能是API调用,它在两个平台上都能做正确的事情,无需根据设备习惯用户切换行为。

是否有更清洁/非设备特定/更惯用的方式来处理在iPhone和iPad上解雇此视图控制器?

我希望并期待self.presentingViewController?.dismiss()为这两种情况工作,但事实并非如此。

我很欣赏演示代码可能需要适应最正确的演示和解雇方法。

ios swift uiviewcontroller uipopovercontroller
1个回答
1
投票

我无法猜测以下是否更干净,但非设备特定,我打算提供其他方法来做到这一点

当将UIViewController呈现为UINavigationController的rootVC时,您可以检查解雇操作

if self.navigationController?.viewControllers.first === self {
    dismiss(animated: true, completion: nil)
} else {
    self.navigationController?.popViewController(animated: true)
}

或者,您可以针对这两种情况执行2个操作

@objc func popAction() {
    self.navigationController?.popViewController(animated: true)
}

@objc func dismissAction() {
    dismiss(animated: true, completion: nil)
}
© www.soinside.com 2019 - 2024. All rights reserved.