如何从另一个 UIViewController 访问一个 UIViewControllers 属性?

发布于 2024-10-17 23:52:34 字数 764 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2024-10-24 23:52:34

从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.

凉风有信 2024-10-24 23:52:34

将所需的子视图控制器公开为包含它们的视图控制器的属性。

将您的根视图控制器公开为应用程序委托的属性,并合成它们。

当您想要访问不同的 UIViewController 时,首先获取对您的 appDelegate 的引用:

MyAppDelegate* myAppDelegate = [[UIApplication sharedApplication] delegate];

然后只需使用您的属性链即可访问您所需的视图控制器。

SubSubViewController* ssvc = myAppDelegate.rootViewController.subViewController.subSubViewController;

有些人不赞成使用 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:

MyAppDelegate* myAppDelegate = [[UIApplication sharedApplication] delegate];

Then just use your chain of properties to get access to your desired view controller.

SubSubViewController* ssvc = myAppDelegate.rootViewController.subViewController.subSubViewController;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文