如何调用代理方法

如何调用代理方法

本文介绍了如何调用代理方法,iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出:

  1. classA及其委托人
  2. classB上有一个按钮
classA.h

@protocol classADelegate <NSObject>
- (void)method:(NSString *) name;
@end
@interface StoreChooser : UIViewController
@end
------------------------------------------------------
classA.m
-(IBAction)buttonCliked{
     // invoke delegate method from classA at here

}

classB.h
@interface classB : UIViewController <classADelegate>
@end

------------------------------------------------------
// Conform delegate of classA
classB.m
- (void)method:(NSString *) name {

} @结尾 -------------------------------------------------- ----

} @end ------------------------------------------------------

我的目标:我需要classB在 buttonClicked 操作

My goal : I need classB to invoke a method delegate from classA in buttonClicked action

问题:我该怎么做才能实现自己的目标.

Question : what should I do to achieve my goal.

推荐答案

只需确保我们在同一页上即可:)

Just to make sure that we are on the same page :)

如果ClassA具有委托ClassADelegate.这意味着当ClassA中发生某个事件"时,ClassA将要通过其委托通知其他类事件"已发生-ClassB. ClassA将通过其委托-ClassADelegate进行此操作.

If ClassA has a delegate ClassADelegate. What this means is that when some "event" occurs in ClassA, ClassA will want to notify some other class via its delegate that the "event" occurred - ClassB. ClassA will do this via its delegate - ClassADelegate.

为此,ClassB必须让ClassA知道它将充当ClassA的委托. ClassB必须通过实现协议中列出的未标记为@optional的所有方法来符合" ClassA的协议.

For this to happen, ClassB will have to let ClassA know that it will be acting as ClassA's delegate. ClassB will have to "conform" to ClassA's protocol by implementing all of the methods listed in the protocol that not marked as @optional.

在代码中,您可以这样做:

In code, you could do this:

// ClassA's delegate

@protocol ClassADelegate <NSObject>
- (void) didDoSomethingCool:(NSString *) name;
@end

// ClassA definition

@interface ClassA
// We'll use this property to call the delegate.
// id<XXX> means that which ever class is assigned to id MUST conform to XXX
@property (nonatomic, assign) id<ClassADelegate> classADelegate;
- (void) doSomething;
@end

// Class A implementation

@implementation ClassA

@synthesize classADelegate;

- (void) doSomething
{
  // Do cool things here.
  // Now call delegate, in this example, this will be ClassB
  [classADelegate didDoSomethingCool:@"Hello from Class A"];
}

现在我们需要连接ClassB,以便可以通知ClassA中发生了某些事情:

Now we need to wire-up ClassB so that it can be notified that something happened in ClassA:

// ClassB definition

@interface ClassB<ClassADelegate>
// ClassB<ClassADelegate> lets the compiler know that ClassB is required to have all the
// non-optional method that are listed in ClassADelegate. In short, we say that
// ClassB conforms to the ClassADelegate.
{
  ClassA *_classA;
}
@end

现在在ClassB的实现文件中的某个位置,我们具有以下内容.

Now somewhere in ClassB's implementation file we have the following.

// ClassB implementation

@implementation ClassB

- (id) init
{
  self = [super init];
  if(self)
  {
    // Just quickly creating an instance of ClassA.
    _classA = [ClassA new];
    // This is were we tell ClassA that ClassB is its delegate.
    _classA.classADelegate = self;
  }
  return self;
}

- (void) dealloc
{
  [_classA release];
  [super dealloc];
}

- (void) didDoSomethingCool:(NSString *) name
{
  // This is the method that ClassA will be calling via the
  // [classADelegate didDoSomethingCool:@"Hello from Class A"] method call.
}

@end

我希望这会有所帮助:)

I hope this helps :)

这篇关于如何调用代理方法,iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:00