从容器视图访问主视图控制器 [英] Access the main view controller from its container view

查看:75
本文介绍了从容器视图访问主视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有容器视图的mainViewController。我正在尝试从容器视图访问theMainViewController。



这是我的代码:

I have a mainViewController with a container view in it. I'm trying to access theMainViewController from the container view.

Here is my code:

self.theMainViewController = (theMainViewController *)self.parentViewController;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[self.theMainViewController .tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];



这不起作用。 self.theMainViewController在我制作nslog时给出一个nil值。然后我换了:




This doesn't work. self.theMainViewController gives a nil value when I make an nslog of it. I then replaced:

self.parentViewController



to:


to:

self presentingViewController



它给了我相同的结果。如何从容器视图的类中访问mainViewController?


and it gave me the same results. How can I access the mainViewController from the container view's class?

推荐答案

它显示了类设计中的一些弱点。您应该坚持使用MVC范例,该范例包括数据,UI和事件处理的分离。



您需要为它定义和设置(弱)属性。我建议创建一个协议和一个委托。
It shows some weakness in your class design. You should stick to the MVC paradigma which inlcudes the separation of data, UI and handling to events.

You need to define and set a (weak) property for it. I would recommend to create a protocol and a delegate.


这篇关于从容器视图访问主视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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