简体   繁体   English

如何调用代理方法,iPhone

[英]how to call delegate method, iphone

Given : 鉴于:

  1. classA with its delegate classA及其委托
  2. classB has a button on it 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 ------------------------------------------------------ } @结束 - - - - - - - - - - - - - - - - - - - - - - - - -------

My goal : I need classB to invoke a method delegate from classA in buttonClicked action 我的目标:我需要classB在buttonClicked动作中从classA调用方法委托

Question : what should I do to achieve my goal. 问题 :我应该怎么做才能实现自己的目标。

Just to make sure that we are on the same page :) 只是为了确保我们在同一页面上:)

If ClassA has a delegate ClassADelegate . 如果ClassA具有委托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发生某个“事件”时, ClassA将要通过其委托通知其他某些类“事件”发生了ClassB ClassA will do this via its delegate - ClassADelegate . ClassA将通过其委托ClassADelegate进行此ClassADelegate

For this to happen, ClassB will have to let ClassA know that it will be acting as ClassA 's delegate. 为此, ClassB必须让ClassA知道它将充当ClassA的委托。 ClassB will have to "conform" to ClassA 's protocol by implementing all of the methods listed in the protocol that not marked as @optional. 通过实施协议中列出的未标记为@optional的所有方法, ClassB必须“符合” ClassA的协议。

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"];
}

Now we need to wire-up ClassB so that it can be notified that something happened in ClassA : 现在我们需要连接ClassB以便可以通知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

Now somewhere in ClassB 's implementation file we have the following. 现在,在ClassB的实现文件中的某处,我们具有以下内容。

// 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 :) 我希望这有帮助 :)

Add an assign property to classA: 将分配属性添加到classA:

@property (nonatomic, assign) id<classADelegate> delegate; 

Then in the viewDidLoad method for classB, call: 然后在classB的viewDidLoad方法中,调用:

[myClassAObject setDelegate:self];

Then in class A, just call: 然后在A类中,只需调用:

if (_delegate && [_delegate respondsToSelector:@selector(method:)]) {
       [_delegate method:@"string"];
}

ClassB needs a reference to ClassA. ClassB需要对ClassA的引用。 This code is not complete but should help you to understand the relationship you need to create. 该代码虽然不完整,但是应该可以帮助您了解需要创建的关系。

classB.h B类

@interface classB: UIViewController <classADelegate>

@property (weak, nonatomic) id<classADelegate> delegate;

@end

classB.m 班级

-(id)init {
    self = [super init];
    if (self) {
        self.delegate = [[classADelegate alloc] init];
    }
}

-(IBAction)buttonClicked {
    [delegate method:@"name"];
}

You need to store the delegate object in classA: 您需要将委托对象存储在classA中:

@interface classA : NSObject
{
    id<classADelegate> _delegate;
}

@property (nonatomic, assign, readwrite) id<classADelegate> delegate;

And synthesize the property: 并综合属性:

@implementation classA
...

@synthesize delegate = _delegate;

And then in the method that needs to call the delegate, you need to test the delegate object and method is valid: 然后在需要调用委托的方法中,您需要测试委托对象和方法是否有效:

- (void)somethingHappened
{
    if ([_delegate respondsToSelector:@selector(method:)])
    {
        [_delegate method:@"Andy"];
    }
}

And finally conform to the delegate protocol in class B: 并最终符合B类中的委托协议:

@implementation classB
...

- (void)method:(NSString *)name
{
    [self buttonCliked:nil];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM