开发者

Calling a function from a different view controller for iphone

I have a problem where I want to call a funct开发者_JAVA百科ion 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


/* 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];


"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.


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:

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

You can also wire the button up in Interface Builder to do the same thing. Or mix and match to your hearts content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜