UIView needs to push a UIViewController (delegation)

烈酒焚心 提交于 2019-12-24 08:59:48

问题


I'm having a hard time getting my head around custom delegation - I would be most grateful if someone could help me:

I have a UIViewController with a tab bar controller, and each of these contains a subclassed UIView. In one of the views, I have a UIButton in which I want to load a UIDocumentInteractionController. I already have the code to do this second part, as the document controller needs access to a UIViewController in order to get pushed to the stack.

But how can my UIView get access to the UIViewController?

I've read a plethora of things on SO tonight, such as:

  • It can't be done
  • Set the UIViewController as a property of the UIView so it knows which it belongs to
  • Calling nextResponder on the UIViewController
  • Delegation

I can happily hit my -(void)buttonPressed: (id)sender method, but where should it go from here to implement delegation? Should it just fire something like [delegate documentRequested: sender] assuming this method exists in my view controller?

Thanks in advance for any assistance.


回答1:


I recently posted a detailed answer on how to use custom delegation. You can check it out here: dismissModalViewController AND pass data back

What you want to do here is implement the protocol in your UIView and set the UIViewController to be it's delegate.

To help you understand the linked answer better, in the answer, secondviewcontroller is equivalent to your UIView and firstviewcontroller is equivalent to your UIViewController.



来源:https://stackoverflow.com/questions/6208361/uiview-needs-to-push-a-uiviewcontroller-delegation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!