从UIView的子视图获取UINavController? [英] Get UINavController from subview of UIView?

查看:228
本文介绍了从UIView的子视图获取UINavController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个自定义子视图到UIViewController的内容视图。从该子视图,我如何获得对superview的控制器的引用?
感谢

I added a custom subview to a UIViewController's content view. From that subview, how can I get a reference to the superview's controller? Thanks

推荐答案

正确的答案是你在做错了; - )

The correct answer is "You're Doing It Wrong™" ;-)

你不需要从一个视图引用一个视图控制器,你绝对不应该在你的视图中保留一个视图控制器 - 或者你会得到一个保留循环和泄漏内存。

You shouldn't need to reference to a view controller from a view, and you certainly should never retain a view controller in one of your views -- or you'll end up with a retain loop and leak memory.

Cocoa提供多种模式来解决这个问题:

Cocoa provides multiple patterns to solve this problem:


  • 使用委托:定义一个名为DemoViewDelegate的协议,并向DemoView添加一个委托属性。然后让你的视图控制器实现这个协议。 重要提示:代表不应保留!您创建的任何委托属性应设置为 assign 请参阅苹果授权文档或只是google授权模式。

  • Use a delegate: Define a protocol called DemoViewDelegate and add a delegate property to DemoView. Then have your view controller implement this protocol. Important: delegates should never be retained! Any delegate property you create should be set to assign. See Apple's Delegation docs or just google "delegation pattern".

使用响应者链:调用UIApplication的 sendAction:to:from:forEvent:,并将改为: nil 将您的操作消息自动路由到响应者链到您的视图控制器。请参见 Apple的响应程序文档以及更详细的操作邮件文档

Use the responder chain: Call UIApplication's sendAction:to:from:forEvent: and leave to: set to nil to have your action message automatically routed up the responder chain to your view controller. See Apple's Responder docs and the more detailed Action Messages docs.

使用通知:在此特定情况下较不常见,但您也可以让视图控制器监听视图发送的通知。

Use a notification: Less common in this particular scenario, but you could also have your view controller listen for a notification, which the view sends.

这篇关于从UIView的子视图获取UINavController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆