0

关于如何在此处进行类之间交互的一个非常基本的问题:如何通过单击链接到另一个类(我的绘图类 - 以编程方式定义)?

谢谢!

编辑:我试图实施下面建议的解决方案,但我没有设法触发其他类的操作。我有两个类:主视图控制器和一个带有绘图代码的类。任何建议将不胜感激。谢谢!

//MainViewController.m
//This class has a xib and contains the graphic user interface

- (void)ImageHasChanged
{        
//do something on the GUI
}


//DrawView.m
//This class has no associated xib and contains the drawing code

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
 //I want to call ImageHasChanged from MainViewController.m here
 //How can I do this?
}
4

3 回答 3

1

Inter-class functionality is done simply by importing one class into the other, and calling an accessible method/instance variable on the import.

For the button IBAction example in your question:

ClassA.m (This will be imported via its header):

#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 (This is your UI class that will call the other method):

#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

Side note: If you're going to require ClassA a lot in ClassB, consider creating a class-wide instance of it in ClassB to re-use wherever it's required. Just don't forget to release it in dealloc (or maybe set it to nil in ARC) when you're finished with it!

Finally, please consider reading through the Apple Docs on Objective-C classes (and indeed all other sections of the documentation relevant to what you're trying to achieve). It is a bit time-consuming, but very well invested in the long run into building your confidence as an Objective-C programmer!

于 2012-08-13T23:39:08.083 回答
0

//正如你所说,必须首先创建一个 MainViewController 的实例

MainViewController *instanceOfMainViewController = [[MainViewController alloc]init];
[instanceOfMainViewController ImageHasChanged];

//感谢您的帮助安德!

于 2012-09-19T05:21:23.577 回答
0

实际上,您可以使用@protocol(Delegate) 在两个类之间交互消息,这是标准方式或参考此文档 http://developer.apple.com/library/ios/#documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources。 html了解更多

于 2012-09-19T09:13:36.923 回答