如何从另一个UIViewController访问一个UIViewControllers属性? [英] How to access one UIViewControllers properties from another UIViewController?

查看:67
本文介绍了如何从另一个UIViewController访问一个UIViewControllers属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有一个MainViewController,它当然是一个主UIView。我的主视图包含许多不同的子视图。

I have one single MainViewController which has of course it's one main UIView. I have this main view comprised of many different subviews.

其中一些子视图具有自己的ViewController。

Some of these subviews have their very own ViewController.

让我们说 MAIN 视图(其委托主要是MainViewController)具有一个容器,该容器加载另一个UIView,该UIView使用单独的UIViewController- 作为大多数操作的委托。

Lets say the MAIN view (whose delegate is primarily MainViewController) has a container which loads another UIView that uses a separate UIViewController- SecondaryViewController as the delegate for most it's actions.

此容器视图当然是通过



This container view is of course loaded in MainViewController via

MyContainerViewController *myContainerController = 
         [[MyContainerViewController alloc] ...]; 
[self addSubView: myContainerController.view];

myContainerController.view的控制器虽然是 MyContainerViewController 。如何在此控制器内部访问MainViewController属性?具体来说,我需要访问MainViewController的-self.navigationController属性以推送新的ViewController吗? :)

the controller for myContainerController.view though is MyContainerViewController. How inside this controller do I access MainViewController properties? Specifically I need to access MainViewController's - self.navigationController property to push a new ViewController? :)

这有意义吗?我认为将要进行某种类型的转换,因为似乎我需要以某种方式在SecondaryViewController中保留对MainViewController的引用?

Does this make any sense? I assume there's going to be casting of some sort involved since it seems I need to somehow retain a reference to MainViewController inside SecondaryViewController?

推荐答案

从MainViewController中的SecondaryViewController推送新的ViewController没有意义。
这会破坏代码的设计。子对象将访问其父方法来调用方法。换句话说:错误的代码。
从SecondaryViewController到其状态已更改的MainViewController进行委托调用。然后MainViewController可以决定执行该调用,SecondaryViewController将不知道有关MainViewController的实现的任何信息。

It doesn't make sense to push a new ViewController from the SecondaryViewController in the MainViewController. This screws up the design of the code. A child object will access its parents method to call a method. By other words: bad code. Make a delegate call from the SecondaryViewController to the MainViewController that it state has changed. Then the MainViewController can decide to do with the call and the SecondaryViewController will not know anything about the implementation of the MainViewController.

因此:
在SecondaryViewController中创建一个协议。
让MainViewController成为SecondaryViewController的委托。
在MainViewController中实现委托方法,该方法将推送新的ViewController。

So: Make a protocol in SecondaryViewController. Let the MainViewController be SecondaryViewController's delegate. Implement the delegate method in MainViewController which pushes the new ViewController.

这篇关于如何从另一个UIViewController访问一个UIViewControllers属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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