我有一个名为UIViewController
的{{1}},其中包含另外两个孩子Container
:UIViewControllers
和Master
。
我的问题是尝试从其中一个子控制器访问Detail
。
App代表代码:
Container
货柜代码:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var container: Container?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
container = Container(nibName: "Container", bundle: nil)
window?.rootViewController = Container()
window?.makeKeyAndVisible()
return true
}
func appShowDetailView(vc:UIViewController){
container!.showDetailView(vc)
}
}
问题在于import UIKit
class Container: UIViewController {
var masterView:UIViewController?
var detailView:UIViewController?
var masterViewFrame = CGRectMake(0, 0, 0, 0)
var detailViewFrame = CGRectMake(0, 0, 0, 0)
var detailOnScreenFrame = CGRectMake(0, 0, 0, 0)
var detailOffScreenFrame = CGRectMake(0, 0, 0, 0)
var masterViewHeight:CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
masterView = MenuViewController(nibName: "MenuViewController", bundle: nil)
detailView = RootViewController(nibName: "RootViewController", bundle: nil)
masterViewFrame = CGRectMake(0, 0, self.view.frame.width, masterViewHeight)
detailViewFrame = CGRectMake(0, masterViewHeight, self.view.frame.width, self.view.frame.height - masterViewHeight)
detailOffScreenFrame = CGRectMake(detailViewFrame.origin.x, detailViewFrame.origin.y + detailViewFrame.height, detailViewFrame.width, detailViewFrame.height)
detailOnScreenFrame = detailViewFrame
self.addChildViewController(masterView!)
self.addChildViewController(detailView!)
masterView!.view.frame = masterViewFrame
detailView!.view.frame = detailViewFrame
self.view.addSubview(masterView!.view)
self.view.addSubview(detailView!.view)
}
func cycleDetailView(from:UIViewController, toView to:UIViewController){
from.willMoveToParentViewController(nil)
self.addChildViewController(to)
to.view.frame = self.detailOffScreenFrame
self.detailView = to
self.transitionFromViewController(from, toViewController: to, duration: 0.25, options: UIViewAnimationOptions.allZeros,
animations: {() -> Void in
// send the new view controller in, and the old one out
to.view.frame = self.detailOnScreenFrame
from.view.frame = self.detailOffScreenFrame
}, completion: {success -> Void in
// tells the view controllers cycling has ended
from.removeFromParentViewController()
to.didMoveToParentViewController(self)
}
)
}
func showDetailView(vc: UIViewController){
if let oldView = detailView {
cycleDetailView(oldView, toView: vc)
}else{
detailView = self.childViewControllers[1] as? UIViewController
if let oldView = detailView {
cycleDetailView(oldView, toView: vc)
}else{
}
}
}
}
。当这个代码被这样的代码调用时:
showDetailView()
...容器函数 let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
if let delegateUnwrapped = appDelegate {
delegateUnwrapped.appShowDetailView(aViewController)
}
只能为showDetailView()
获取nil值,而detailView
是一个空数组。
这让我觉得我以错误的方式访问子视图控制器。
谁能告诉我哪里出错了?
TIA。
答案 0 :(得分:3)
我认为你的问题是app delegate中的这一行,
window?.rootViewController = Container()
这将创建一个Container的新实例,而不是您分配给容器属性的实例。将该行更改为,
window?.rootViewController = container