Iphone 从子类调用视图

Iphone 从子类调用视图,iphone,uiviewcontroller,delegates,subview,Iphone,Uiviewcontroller,Delegates,Subview,我有一个iPhone应用程序,主要由两个视图组成,我们称它们为全屏视图和卡片视图(卡片视图显示为全屏视图的子视图)。通过使用mastercardwiewclass和masterFullScreenViewClass,我已经处理了显示卡片视图的所有动画。所有特定的CardView都是masterCardView的子类,所有特定的FullScreenView都是masterFullScreenView的子类 我使用masterFullScreenViewClass中的方法展示了一个cardView。

我有一个iPhone应用程序,主要由两个视图组成,我们称它们为
全屏视图
卡片视图
卡片视图
显示为全屏视图的子视图)。通过使用
mastercardwiewclass
masterFullScreenViewClass
,我已经处理了显示卡片视图的所有动画。所有特定的CardView都是masterCardView的子类,所有特定的FullScreenView都是masterFullScreenView的子类

我使用
masterFullScreenViewClass
中的方法展示了一个cardView。我使用masterCardView中的委托方法解除cardView。但是,调用方法从另一个cardView显示cardView时遇到问题。所有cardView呈现方法都包含在masterFullScreenViewController类中


我如何访问这些方法而不复制一个文件,并在需要的地方本地粘贴它们。

一种方法是使用协议

简而言之,您的
masterCardView类
将实现一个表示
cardView
的协议方法(为了简单起见,假设您使用某个索引调用cardView):

masterCardView.h:

@protocol CardPresenterDelegate <NSObject>

- (void)presentCardViewWithIndex:(int)index;

@end

@interface MasterCardView:UIViewController <CardPresenterDelegate>
...
- (void)presentCardViewWithIndex:(int)index
{
// Code for presenting a cardView
}
cardView.cardPresenterDelegate = self;
您还需要在cardView中创建委托(弱)属性:

cardwiew.h

@property (weak) id<CardPresenterDelegate> cardPresenterDelegate;
哦,还有,在masterCardView中创建CardView时,不要忘记在CardView上设置代理属性:

回到masterCardView.m:

@protocol CardPresenterDelegate <NSObject>

- (void)presentCardViewWithIndex:(int)index;

@end

@interface MasterCardView:UIViewController <CardPresenterDelegate>
...
- (void)presentCardViewWithIndex:(int)index
{
// Code for presenting a cardView
}
cardView.cardPresenterDelegate = self;