类实例不同,使用哪一个?

发布于 2024-10-08 03:45:41 字数 1159 浏览 0 评论 0原文

我被以下问题困住了。在程序中,我试图在不同的类之间进行通信(带有 TabBar 应用程序中附加的 NIB 文件的视图控制器等)。我想在名为“ProductViewDetailController”的类中调用方法“OMFG”。这个类是一个UIViewController(SplitViewDelegate)。它以编程方式加载。

无论如何,我一直在尝试正确调用该控制器,并提出了 2 个解决方案。一种是在调用者的.h文件和.m文件中声明productviewdetailcontroller,制作一个IBOutlet,在Interface构建器中链接它并直接通过行调用它

[productDetailController OMFG];

当我调用此方法时,它调用ProductViewDetailController中的正确方法,但是此视图控制器的实例与我可以通过以下代码以编程方式访问的实例不同:

for (UIViewController *controller in self.tabBarController.viewControllers) {
      NSLog(@"%@", [controller class]);
      if ([controller isKindOfClass:[UISplitViewController class]]) {
       UISplitViewController *cell = (UISplitViewController *)controller;
       for (UIViewController *controller2 in cell.viewControllers) {
        NSLog(@"%@", [controller2 class]);
        if ([controller2 isKindOfClass:[ProductViewDetailController class]]) {
         [controller2 OMFG];

        }
       }
      }

我应该使用哪一个,为什么?

编辑:当我尝试向两个视图控制器添加子视图时,调用的是 [controller2 OMFG];实际上显示了新添加的视图,其中 [productDetailController OMFG];不显示新添加的视图...这是为什么?是否有更短(更别致)的方式来访问正确的 ViewController?

I'm stuck with the following. In a program, I'm trying to communicate between different classes (View Controllers with NIB files attached in a TabBar application etc). I want to call a method 'OMFG' in a class called 'ProductViewDetailController'. This class is a UIViewController (SplitViewDelegate). It's loaded programmatically.

Anyways, I've been trying to get the right call to this controller, and I came up with 2 solutions. One is declaring the productviewdetailcontroller in the caller's .h file and .m file, making an IBOutlet, linking it in the Interface builder and calling it directly by the line

[productDetailController OMFG];

When I call this method, it calls the right method in the ProductViewDetailController, but the instance of this viewcontroller differs from the one I programmatically can reach with this code:

for (UIViewController *controller in self.tabBarController.viewControllers) {
      NSLog(@"%@", [controller class]);
      if ([controller isKindOfClass:[UISplitViewController class]]) {
       UISplitViewController *cell = (UISplitViewController *)controller;
       for (UIViewController *controller2 in cell.viewControllers) {
        NSLog(@"%@", [controller2 class]);
        if ([controller2 isKindOfClass:[ProductViewDetailController class]]) {
         [controller2 OMFG];

        }
       }
      }

Which one should I use, and why?

edit: When I try to add a SubView to both viewcontrollers, the one where the call is [controller2 OMFG]; actually shows the newly added view, where the [productDetailController OMFG]; doesn't show the newly added view... Why is that? Is there a shorter (and more chique) way to get access to the right ViewController?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

牵你手 2024-10-15 03:45:41

您应该使用IBOutlet。这可以确保您的应用程序仍然可以在您稍后决定更改视图控制器的层次结构时调用正确的目标,例如,如果创建不带 UISplitViewController 的 iPhone 兼容设置。

在 Objective-C 中调用 isKindOfClass: 明确表明您所做的事情可能是错误的。首先,在 Cocoa Touch 中,你做什么总是比你是谁更重要。其次,你试图做的可能是窥视一些应该保密的东西。

You should use a IBOutlet. This makes sure your app can still call the correct target if you later decide to change the hierarchy of view controllers, for example if creating an iPhone compatible setup without a UISplitViewController.

Calling isKindOfClass: in Objective-C is a sure sign that what you are doing is probably wrong. Firstly in Cocoa Touch what you do is always more important than who you are. Secondly what you try to do is probably peeking inside something that should be left private.

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