获取当前ViewController的标题

时间:2018-03-05 18:31:02

标签: ios swift uiviewcontroller storyboard viewcontroller

我想将当前加载的ViewController的名称作为String。

我得到的代码工作得非常好:

    if let window = UIApplication.shared.delegate?.window {
        var viewcontroller = window!.rootViewController
        if(viewcontroller is UINavigationController){
            viewcontroller = (viewcontroller as! UINavigationController).visibleViewController

        }
        print(String(describing: viewcontroller!))
    }

但结果看起来不像这样:

 "MyViewController1"

它看起来像这样:

<myapplication.MyViewController1: 0x30141e210>

如何获取ViewControllers名称/标题?必须有一些更有效的方法,然后在"." and ":"

之间拆分字符串

编辑:“vc!.nibName”无效,因为我正在使用故事板!

编辑2:这就是结果应该是这样的:"MyViewController1"

编辑3: print(viewcontroller?.title)也无效。它只是返回“nil”

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

如果让titleString = viewController.navigationItem.title { 打印(titleString) }

答案 1 :(得分:-1)

此函数将返回currentViewController的名称:

/// Returns The Class Name Of A Given View Controller
///
/// - Parameter viewController: UIViewController
/// - Returns: String
func classNameFrom(_ viewController: UIViewController) -> String{

    let currentViewControllerName = NSStringFromClass(viewController.classForCoder).components(separatedBy: ".").last!

    print("Current View Controller = \(currentViewControllerName)")

    return currentViewControllerName

}

您可以在ViewDidLoad等中致电:

print(classNameFrom(self))

第二种方法,也许是一种更强大的解决方案:

extension UIViewController {

/// Returns The Top Most ViewController In The Navigation Controller
///
/// - Parameter base: UIViewController
/// - Returns: UIViewController
func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

    if let navigationController = base as? UINavigationController {
        return topViewController(base: navigationController.visibleViewController)
    }
    if let tabBarController = base as? UITabBarController {
        if let selected = tabBarController.selectedViewController {
            return topViewController(base: selected)
        }
    }
    if let presentedViewController = base?.presentedViewController {
        return topViewController(base: presentedViewController)
    }
    return base
}

/// Returns The Class Name Of A Given View Controller
///
/// - Parameter viewController: UIViewController
/// - Returns: String
func classNameFrom(_ viewController: UIViewController) -> String{

    let currentViewControllerName = NSStringFromClass(viewController.classForCoder).components(separatedBy: ".").last!

    print("Current View Controller = \(currentViewControllerName)")

    return currentViewControllerName

    }
 }

可以这样称呼:

  guard let thisViewController = topViewController(base: self) else { return }
        print(classNameFrom(thisViewController))

答案 2 :(得分:-2)

看起来没人理解我的问题。这就是我现在使用这个&#34; noob&#34;溶液:

   if let window = UIApplication.shared.delegate?.window {
        var viewcontroller = window!.rootViewController
        if(viewcontroller is UINavigationController){
            viewcontroller = (viewcontroller as! UINavigationController).visibleViewController
        }
        let viewControllerString = String(describing: viewcontroller!).split(separator: ".")[1].split(separator: ":")[0]
        print(viewControllerString)
    }

我希望有人可以正确回答这个问题,只要我们可能不得不忍受这些代码行!