如何获得作为呈现视图控制器的顶视图

时间:2016-06-25 08:03:06

标签: ios swift uiview

我有一个导航栏控制器。在一些动作之后,我需要弹出一个新视图,这是一个呈现的视图控制器。然后我如何获得顶视图?

我总是在导航堆栈上获得顶视图,但不是所呈现的视图。为什么呢?

@extends('layouts.dashboardmaster')

$quotetext = "quotes";

if ( $quotes == 1)
    { $quotetext = "quote"; }



@section('pageheading')
    <div class="well">
        <h2><i class = "fa fa-quote-left"></i> edit author ({{$author->author}}), ({{ number_format($quotes,0) }} {{}}$quotetext }}) </h2>
    </div>
@endsection

2 个答案:

答案 0 :(得分:1)

尝试

if let topController = myNavigationController.visibleViewController {
    print(topController)
}

答案 1 :(得分:0)

Swift 3示例让顶级UIViewController呈现另一个UIViewController:

import UIKit

extension UIApplication {
    var topVC: UIViewController? {
        get {
            let rootVC = UIApplication.shared.keyWindow?.rootViewController

            if let nav = rootVC as? UINavigationController {
                return nav.visibleViewController
            } else if let tab = rootVC as? UITabBarController, let selected = tab.selectedViewController {
                return selected
            } else if let presented = rootVC?.presentedViewController {
                return presented
            }
            return rootVC
        }
    }
}

可以用:

来调用它
_ = UIApplication.shared.topVC?.present(yourViewController, animated: true)