首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewController present并不总是有效

ViewController present并不总是有效
EN

Stack Overflow用户
提问于 2021-11-23 08:27:57
回答 1查看 50关注 0票数 0

我在IOS应用程序中显示视图控制器时遇到一些问题。有时它会工作并显示视图,但有时,我猜当上下文稍有不同时,它就不会工作了。调试器中没有错误或警告,并且它可以从用于self.present的主情节提要中找到ViewController (至少它不是空的),但这似乎不再起作用。

代码语言:javascript
复制
  @IBAction func showHistoryButton(_ sender: MDCButton) {
        let exercisesHistoryVC = ExercisesHistoryViewController.instantiate(from: .Main)
        exercisesHistoryVC.modalPresentationStyle = .fullScreen
        let appDeligate = UIApplication.shared.delegate as! AppDelegate
        appDeligate.window?.rootViewController!.present(exercisesHistoryVC,animated: true,completion: nil)
       // parent?.present(exercisesHistoryVC, animated: true, completion: nil)
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-23 10:10:52

使用如下代码,同时显示New View Controller

代码语言:javascript
复制
@IBAction func showHistoryButton(_ sender: MDCButton) {
        let exercisesHistoryVC = ExercisesHistoryViewController.instantiate(from: .Main)
        exercisesHistoryVC.modalPresentationStyle = .fullScreen
        UIApplication.topViewController()?.present(exercisesHistoryVC, animated: false, completion: nil)
 }

extension UIApplication {
    
    static func topViewController(base: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
            return topViewController(base: selected)
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }
    
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70077561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档