如何调用委托方法,iphone

时间:2012-05-09 15:41:33

标签: iphone objective-c

鉴于:

  
      
  1. classA及其代理
  2.   
  3. classB上有一个按钮
  4.   
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 {

}     @结束     -------------------------------------------------- ----

我的目标:我需要classB在 buttonClicked 操作中调用classA中的方法委托

问题:我该怎样做才能实现目标。

4 个答案:

答案 0 :(得分:4)

只是为了确保我们在同一页面上:)

如果ClassA有委托ClassADelegate。这意味着当ClassA中发生某些“事件”时,ClassA会希望通过其代理通知其他类发生“事件” - ClassBClassA将通过其代理人ClassADelegate执行此操作。

要实现这一点,ClassB必须让ClassA知道它将作为ClassA的代理人。 ClassB必须通过实现协议中列出的未标记为@optional的所有方法来“符合”ClassA的协议。

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

// 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中发生的事情:

// 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的实施文件中,我们有以下内容。

// 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

我希望这会有所帮助:)

答案 1 :(得分:1)

为classA添加assign属性:

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

然后在classB的viewDidLoad方法中,调用:

[myClassAObject setDelegate:self];

然后在A班,请致电:

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

答案 2 :(得分:1)

ClassB需要对ClassA的引用。此代码不完整,但应该有助于您了解需要创建的关系。

classB.h

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

答案 3 :(得分:0)

您需要在classA中存储委托对象:

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

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

并合成属性:

@implementation classA
...

@synthesize delegate = _delegate;

然后在需要调用委托的方法中,需要测试委托对象和方法是否有效:

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

最后符合B类中的委托协议:

@implementation classB
...

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