2010-08-31 22 views

回答

1

根據您要調用的函數類型,您可以創建一個靜態函數。爲此,只需聲明如+ (void)printA:(int)a andB:(int)b;

的功能,並在沒有實例的情況下使用[Class2 printA:a andB:b]進行調用。或者,如果您想調用非靜態方法,則可以使用委派協議。

您可以創建一個像

@protocol PrintJobReceiver 
- (void)printVariableA:(int)a andB:(int)b; 
@end 

的協議將其交由Class2中

@interface Class2: NSObject <PrintJobReceiver> { } 
@end 
@implementation Class2 
- (void)printVariableA:(int)a andB:(int)b 
{ 
    NSLog(@"a: %i b:%i",a,b); 
} 
@end 

效法,並設置爲Class1一個委託,那麼你將用它來打印(您想通過一個實例的類2)

@interface Class1:NSObject 
{ 
    id<PrintJobReceiver> delegate; 
} 
@property (nonatomic,readwrite, retain) id<PrintJobReceiver> delegate; 
@end 
@implementation Class1 
@synthesize delegate; 
@end 

然後你可以撥打在Class1內210。我通常使用這個而不是傳遞一個參考到整個班級,以便只顯示班級應該使用的功能(並且你不想使用你不應該使用的功能)