I have a problem where I want to call a function defined in one view controller from another controller. I'v try what seems llke a hundred different setups and nothing seems to work.
I've posted the basic code and was hoping someone could tell me how they would do it. Basically all I want to do is call the MYBPress function defined in the SwitchViewController from the GameViewController when the dealB button is pressed. Any help would be greatly appreciated. PS: I have coded for a long time, but am realtivly new to Obj-C
// ------- SwitchViewController.h ---------------
#import <UIKit/UIKit.h>
@class GameViewController;
@class OptionsViewController;
@interface SwitchViewController : UIViewController {
OptionsViewController *optionsViewController;
}
@property ( retain, nonatomic ) OptionsViewController *optionsViewController;
@property ( retain, nonatomic ) GameViewController *gameViewController;
-(IBAction)MyBPress:(id)sender;
@end
// -------- GameViewController.h ------------
#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController {
IBOutlet UIButton *dealB;
}
@property(nonatomic,retain) IBOutlet UIButton *dealB;
- (IBAction)dealB:(id)sender;
@end
// ------- GameViewController.m
#import "GameViewController.h"
@implementation GameViewController
@synthesize dealB; // The Deal button
- (IBAction)dealB:(id)sender
{
// Here is where I want to call the MyBPress function
}
@end
Why not just have a button send both messages directly to their respective controllers? Instances of UIButton are inherently capable of sending multiple messages to multiple targets. To configure the button, you can send the following message as many times as necessary:
You can also wire the button up in Interface Builder to do the same thing. Or mix and match to your hearts content.
"Hi All, I have a problem where I want to call a function defined in one view controller from another controller. I'v try what seems llke a hundred different setups and nothing seems to work."
This is because it is bad design. Controllers should not directly talk to each other.
You should consider using delegates, notifications or some shared central entity.