从其子 UIViewControllers 访问父 UIViewController
Posted
技术标签:
【中文标题】从其子 UIViewControllers 访问父 UIViewController【英文标题】:Accessing a parent UIViewController from its children UIViewControllers 【发布时间】:2015-02-09 16:20:10 【问题描述】:我有一个名为Container
的UIViewController
,里面有另外两个孩子UIViewControllers
:Master
和Detail
。
我的问题是尝试从其子控制器之一访问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()
只能获取detailView
的nil 值,而self.childViewControllers
是一个空数组。
这让我觉得我以错误的方式访问子视图控制器。
谁能告诉我哪里出错了?
TIA。
【问题讨论】:
【参考方案1】:我认为您的问题是应用程序委托中的这一行,
window?.rootViewController = Container()
这会创建一个新的 Container 实例,而不是您分配给 container 属性的那个实例。将该行更改为,
window?.rootViewController = container
【讨论】:
以上是关于从其子 UIViewControllers 访问父 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章
IOS 如何从其子级访问 uipageviewcontroller?
将相同的 UIBarButtonItem 添加到几个 UIViewControllers
如何向所有 UIViewControllers 添加一个通用的浮动操作按钮