Calling a function from another view controller for iphone

I have a problem when I want to call a function defined in one view controller from another controller. I'm trying to figure out what seems like a hundred different installations and nothing works.

I posted the base code and was hoping someone would tell me how they would do it. Basically, all I want to do is call the MYBPress function defined in SwitchViewController from the GameViewController when the dealB button is clicked. Any help would be greatly appreciated. PS: I coded for a long time, but I'm really 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
+5
source share
3 answers

" , , , , . , , ".

, . .

, - .

+5
/* Ok, so based on a suggestion to use Notifications, I solved my problem.  It's
actually so simple, it ridiculous I had so much trouble with it.   Thought I'd 
post it just in case some other newbie hits the same type issue.
*/

// in the SwitchViewController.m  I added this.  This sets it up so 
// when the dealNotification
// is triggered, the HideBar function I have defined in SwitchViewController 
// gets called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(HideBar) name:@"dealNotification" object: nil];


// in the GameViewController.m where I want to call the function in the other controller,
// I added this and it send the notification to run the function
[[NSNotificationCenter defaultCenter] postNotificationName:@"dealNotification" object: nil];
+25

? UIButton . , , :

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

Interface Builder, . .

+1
source

All Articles