简体   繁体   中英

Attempting to get reference to Container View Controller

I'm currently attempting to re-work this navigation dropdown menu so that the dropdown menu takes up only the bottom half of the window.

Here's my storyboard setup:

故事板

Don't worry about the extraneous labels/buttons. I just wanted to show the different view controllers / connections with this pic.)

I'm having an issue getting a reference to the container view controller so that I can define the frame. Here's what I've got now:

private var navigationController: UINavigationController?

And when declaring the variable:

self.navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController?.presentedViewController?.childViewControllers[0] as? UINavigationController
println(self.navigationController!.nameOfClass) // logs: UINavigationController
self.navigationController = self.navigationController!.topMostViewController?.navigationController // 
println(self.navigationController!.nameOfClass) // logs: Optional(<UINavigationController: 0x7f8681e7b5f0>)UINavigationController

Here's where I get the error - when attempting to define a frame for the dropdown menu view:

var frame = CGRectMake(0, 0, titleSize.width + (self.configuration.arrowPadding + self.configuration.arrowImage.size.width)*2, self.navigationController!.navigationBar.frame.height)

I get the error:

fatal error: unexpectedly found nil while unwrapping an Optional value.

I know what this means, but I'm confused as to why the value is nil. Because when I log the classname of self.navigationController, I get the correct navigation controller.

Also, here's the reference for nameOfClass and topMostViewController:

extension UIViewController {
  var topPresentedViewController: UIViewController? {
    var target: UIViewController? = self
    while (target?.presentedViewController != nil) {
        target = target?.presentedViewController
    }
    print(target)
    return target
  }
  var topVisibleViewController: UIViewController? {
    if let navigation = self as? UINavigationController {
        if let visibleViewController = navigation.visibleViewController {
            return visibleViewController.topVisibleViewController
        }
    }
    if let tab = self as? UITabBarController {
        if let selectedViewController = tab.selectedViewController {
            return selectedViewController.topVisibleViewController
        }
    }
    return self
  }
  var topMostViewController: UIViewController? {
    return self.topPresentedViewController?.topVisibleViewController
  }
}

public extension NSObject{
  public class var nameOfClass: String{
    return NSStringFromClass(self).componentsSeparatedByString(".").last!
  }
  public var nameOfClass: String{
    return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
  }
}

I tried to be concise while still giving sufficient information, but if needed I can post the full project. Any help would be greatly appreciated :)

UIViewController already defines a navigationController property https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/navigationController

I don't know where your code is that does self.navigationController!.navigationBar ... but perhaps its scope has it getting Cocoa's property of that name, and not your own. I don't know why the compiler would have let you redeclare a property with the same name without emitting a warning, something about yours being private?

Try using a different name for your property. Also, take a look at UIViewController 's presentingViewController because maybe you can just use that instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM