國際級的功能是通過導入一個類到另一個,並呼籲進口的訪問方法/實例變量簡單地完成。
對於你的問題按鈕IBAction爲例如:
ClassA.m(這將通過其頭部被導入):
#import "ClassA.h"
@implementation ClassA
// This is a class-level function (indicated by the '+'). It can't contain
// any instance variables of ClassA though!
+(void)publicDrawingFunction:(NSString *)aVariable {
// Your method here...
}
// This is a instance-level function (indicated by the '-'). It can contain
// instance variables of ClassA, but it requires you to create an instance
// of ClassA in ClassB before you can use the function!
-(NSString *)privateDrawingFunction:(NSString *)aVariable {
// Your method here...
}
@end
ClassB.m(這是您的UI類將調用其他方法):
#import "ClassA.h" // <---- THE IMPORTANT HEADER IMPORT!
@implementation ClassB
// The IBAction for handling a button click
-(IBAction)clickDrawButton:(id)sender {
// Calling the class method is simple:
[ClassA publicDrawingFunction:@"string to pass to function"];
// Calling the instance method requires a class instance to be created first:
ClassA *instanceOfClassA = [[ClassA alloc]init];
NSString *result = [instanceOfClassA privateDrawingFunction:@"stringToPassAlong"];
// If you no longer require the ClassA instance in this scope, release it (if not using ARC)!
[instanceOfClassA release];
}
@end
注意:如果您要去請求ClassB在ClassB中很多,可以考慮在ClassB中創建一個Class類的實例,以便在需要的地方重新使用它。當你完成它時,不要忘記在dealloc中釋放它(或者可能將它設置爲ARC中的nil
)!
最後,請考慮閱讀Apple Docs on Objective-C classes(實際上與您試圖實現的相關文檔的所有其他部分)。這有點費時,但從長遠來看投入很大,可以增強您作爲Objective-C程序員的信心!
你能扔了一些代碼嗎?我不是100%確定你的情況是什麼。如果您將相關代碼插入到您的問題中,我們將能夠爲您提供更多幫助。謝謝! – WendiKidd 2012-08-13 23:38:49
我已經添加了一些代碼。謝謝。 – jeddi 2012-09-18 21:19:34