如何从另一个 UIViewController 访问一个 UIViewControllers 属性?
我有一个 MainViewController,当然它是一个主 UIView。我的主视图由许多不同的子视图组成。
其中一些子视图有自己的 ViewController。
假设 MAIN 视图(其委托主要是 MainViewController)有一个容器,该容器加载另一个 UIView,该 UIView 使用单独的 UIViewController - SecondaryViewController 作为大多数操作的委托。
当然,这个容器视图是通过
MyContainerViewController *myContainerController =
[[MyContainerViewController alloc] ...];
[self addSubView: myContainerController.view];
myContainerController.view 的控制器加载到 MainViewController 中的,尽管它是 MyContainerViewController。如何在该控制器内部访问 MainViewController 属性?具体来说,我需要访问 MainViewController 的 - self.navigationController 属性来推送新的 ViewController? :)
这有什么意义吗?我认为将会涉及某种类型的转换,因为我似乎需要以某种方式在SecondaryViewController 中保留对MainViewController 的引用?
I have one single MainViewController which has of course it's one main UIView. I have this main view comprised of many different subviews.
Some of these subviews have their very own ViewController.
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];
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? :)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从MainViewController 中的SecondaryViewController 推送新的ViewController 是没有意义的。
这搞砸了代码的设计。子对象将访问其父对象的方法来调用方法。换句话说:糟糕的代码。
从SecondaryViewController 到MainViewController 进行委托调用,表明其状态已更改。然后 MainViewController 可以决定如何处理该调用,而 secondaryViewController 将不知道有关 MainViewController 的实现的任何信息。
所以:
在SecondaryViewController中制定一个协议。
让MainViewController 成为SecondaryViewController 的委托。
在 MainViewController 中实现推送新 ViewController 的委托方法。
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.
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 时,首先获取对您的 appDelegate 的引用:
然后只需使用您的属性链即可访问您所需的视图控制器。
有些人不赞成使用 sharedApplication 类方法来获取对委托的引用,但我已经在多个应用程序中使用了它,并且还没有受到影响。另一种方法是通过视图控制器的层次结构来传输您的 appDelegate。
Expose the desired sub-view controllers as properties of the view controller that contains them.
Expose your root view controller(s) as properties of your app delegate, and synthesize them also.
When you want to access a different UIViewController, first obtain a reference to your appDelegate:
Then just use your chain of properties to get access to your desired view controller.
Some folks frown upon the use of the sharedApplication class method to obtain the reference to a delegate, but I've used it in multiple apps and not suffered for it (yet). The alternative is to pipe your appDelegate through your hierarchy of viewControllers.