的簡單的解決方案是在將它作爲一個參數:
[instance method:self];
...
- (void) method:(class *)caller { ...
爲了避免耦合兩個類然而,通常使用協議來定義回調的語義,並通過首先分配委託,然後調用方法來將方法調用與回調處理程序的規範分開。這有點牽扯,我希望我已經正確地覆蓋了所有的細節。
// Foo.h
@class Foo;
@protocol FooDelegate
- (void)doSomethingWithFoo:(Foo*)caller;
@end
@interface Foo {
id<FooDelegate> delegate;
}
@property (nonatomic, retain) id<FooDelegate> delegate;
- (void)method;
@end
// Foo.m
@implementation Foo
@synthesize delegate;
- (void)method {
[delegate doSomethingWithFoo:self];
}
@end
// Bar.h
#import "Foo.h"
@interface Bar<FooDelegate> {
}
// Bar.m
@implementation Bar
- (IBAction)method {
Foo *foo = [[Foo alloc] init...];
foo.delegate = self;
[foo method];
}
- (void)doSomethingWithFoo:(Foo*)caller {
NSLog(@"Callback from %@", caller);
}
@end
你是什麼意思?什麼是「價值」和「someOtherValue」? – kennytm 2010-10-24 07:58:42
值將是變量的原始值,其他值將是將分配給實例的新值。你的意思是什麼具體的價值觀? – bubster 2010-10-24 08:38:21