两个视图控制器可以相互代表吗? [英] Can two view controllers be delegates for one another?

查看:92
本文介绍了两个视图控制器可以相互代表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在建立应用程序的同时,我正在学习目标c和可可触摸,所以我有一个相当简单的问题(我想),对不起,网站上没有足够的布朗尼点显示我的网站的视觉图。

I'm learning objective-c and cocoa touch at the same time as building an app so I've got a fairly simple question (I think), sorry I don't have enough brownie points on the site to show a visual diagram of my site.

但是让我解释一下,我有以下

But let me explain, I have the following


  • 主ViewController

  • ViewController A

  • ViewController B

  • ViewController C

  • ViewController 1

  • ViewController 2

  • Main ViewController
  • ViewController A
  • ViewController B
  • ViewController C
  • ViewController 1
  • ViewController 2

ViewController A,B和C都有Main ViewController正在使用的协议代理。

ViewController 1和2具有正在被ViewController C使用的协议委托。

ViewController A, B and C all have protocol delegates that is being used by Main ViewController.
ViewController 1 and 2 have protocol delegates that is being used by ViewController C.

然而,我也需要ViewController C作为主ViewController的代理。

因此,我最近在Main ViewController中创建了一个协议委托,但是当尝试分配ViewController C作为代理我是getti导致错误,更特别的是尝试将Main ViewController的头文件导入View Controller C由于以下原因,我收到一个失败的编译消息

Because of this I recently created a protocol delegate within Main ViewController, however when trying to assign ViewController C as the delegate I'm getting errors, more specially when trying to import the header file of Main ViewController into View Controller C I'm getting a failed compilation message for the following reasons


  • 主ViewControllers头文件可以
    不再找到
    的协议ViewController C

  • 在ViewController C之前的预期说明符-qualifier-list

我正在尝试完成什么?两个视图控制器bd可以相互代表吗?如果不是一个完成我正在做什么的想法方式?

在ViewController CI中尝试了一种替代方法,有一种方法可以调用Main ViewController创建一个父视图控件的指针,创建ViewController B的一个实例(这就是为什么我要让代理工作)然后将ViewControler B的视图添加到我创建的新指针,这个编译但不起作用(我会尝试并且找出原因,这不是我在这里问的)。

Within ViewController C I tried an alternative, having a method to call Main ViewController by creating a pointer of the parent view controller, creating an instance of ViewController B (this is why I want the delegate to work) then adding ViewControler B's view to the new pointer I created, this compiles but doesn't work (I'll try and find out why, this isn't what I'm asking here).

非常感谢,我真的很欣赏这个网站一段时间了。

Many thanks, I'm really appreciating this site for a while now

推荐答案

这个气味好坏的应用程序设计。但是要解决你的编译错误,你只需要这样做:

This smells like bad application design. But to resolve your compile error you simply do something like this:

@protocol SomeDelegate;

@interface FooThatUsesSomeDelegate : NSObject {
    id<SomeDelegate> delegate_;
}
@end

转发声明 SomeDelegate 您不必在头文件中包含完整声明的位置。这对于两个类依赖于彼此的情况非常有用。

By forward declaring SomeDelegate you do not have to include the header file where it is it fully declared. This is very useful for cases where two classes depend on each other.

您必须将头文件包含在实现源文件中。但这通常不是问题。

You will have to include the header file in the implementation source file. But that is usually not a problem.

这篇关于两个视图控制器可以相互代表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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