0

我有一个主要由两个视图组成的 iPhone 应用程序,我们称它们为fullScreencardViews(它们cardViews作为全屏视图的子视图呈现)。masterCardViewClass我已经通过使用 a和 a来处理呈现卡片视图的所有动画masterFullScreenViewClass。所有特定的cardViews都是masterCardView的子类,所有特定的fullScreenViews都是masterFullScreenView的子类。

我使用来自masterFullScreenViewClass. 我使用 masterCardView 中的委托方法关闭了 cardView。但是,我在调用从另一个 cardView 呈现 cardView 的方法时遇到问题。所有 cardView 呈现方法都包含在 masterFullScreenViewController 类中。

我如何访问这些方法,而无需在需要它们的地方复制粘贴它们。

4

1 回答 1

1

一种方法是使用协议。

简而言之,您masterCardView class将实现一个提供 a 的协议方法cardView(为了简单起见,假设您调用具有特定索引的 cardViews):

万事达卡视图.h:

@protocol CardPresenterDelegate <NSObject>

- (void)presentCardViewWithIndex:(int)index;

@end

@interface MasterCardView:UIViewController <CardPresenterDelegate>
...

万事达卡视图.m:

- (void)presentCardViewWithIndex:(int)index
{
// Code for presenting a cardView
}

您还需要在您的 cardView 中创建一个委托(弱)属性:

卡片视图.h

@property (weak) id<CardPresenterDelegate> cardPresenterDelegate;

然后通过访问您的 cardView 中的该属性,您可以告诉 masterCardView 为您做一些事情:

卡片视图.m

[self.cardPresenterDelegate presentCardViewWithIndex:5];

哦,而且,在你的 masterCardView 中创建它们时,不要忘记在你的 cardViews 上设置委托属性:

回到masterCardView.m:

cardView.cardPresenterDelegate = self;
于 2012-10-16T23:49:23.400 回答