1

给定:

  1. A 类及其代表
  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 {

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

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

问题:我应该怎么做才能实现我的目标。

4

4 回答 4

4

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

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

为此,ClassB必须ClassA告知它将充当ClassA' 代表。ClassB必须ClassA通过实现协议中列出的所有未标记为@optional 的方法来“符合”协议。

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

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

我希望这有帮助 :)

于 2012-05-09T16:16:28.250 回答
2

给classA添加一个assign属性:

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

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

[myClassAObject setDelegate:self];

然后在 A 类中,只需调用:

if (_delegate && [_delegate respondsToSelector:@selector(method:)]) {
       [_delegate method:@"string"];
}
于 2012-05-09T15:47:13.397 回答
1

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

B类.h

@interface classB: UIViewController <classADelegate>

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

@end

B类.m

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

-(IBAction)buttonClicked {
    [delegate method:@"name"];
}
于 2012-05-09T15:49:29.963 回答
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];
}
于 2012-05-09T15:48:12.523 回答