jeddi
jeddi

Reputation: 651

How to interact between classes

A very basic question on how to interact between classes here: how can I trigger an action called by clicking on a button linked to one class (the graphic user interface in my case - which does not contain any drawing code) inside another class (my drawing class - which is defined programmatically)?

Thanks!

Edited: I have tried to implement the solutions suggested below but I didn't manage to trigger the action from the other class. I have two classes: the main view controller and a class with the drawing code. Any advice would be highly appreciated. Thanks!

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

Upvotes: 0

Views: 122

Answers (3)

SachinVsSachin
SachinVsSachin

Reputation: 6427

Actually you can use @protocol(Delegate) to interact message between two classes this is standard way Or refer this document http://developer.apple.com/library/ios/#documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html to learn more

Upvotes: 0

jeddi
jeddi

Reputation: 651

//As you said an instance of MainViewController has to be created first

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

//Thanks for your help Andeh!

Upvotes: 0

andycam
andycam

Reputation: 1692

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!

Upvotes: 1

Related Questions