从iphone的不同视图控制器调用功能

时间:2021-12-21 20:33:46

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

我发布了基本代码,并希望有人能告诉我他们将如何做到这一点。基本上我想要做的就是当按下dealB按钮时,从GameViewController调用SwitchViewController中定义的MYBPress函数。任何帮助将不胜感激。 PS:我已经编写了很长时间,但对于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

3 个解决方案

#1


5  

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

您应该考虑使用代理,通知或某些共享的*实体。

#2


25  

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

#3


1  

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:

为什么不让一个按钮直接将两个消息发送到各自的控制器? UIButton的实例本身就能够向多个目标发送多条消息。要配置按钮,您可以根据需要多次发送以下消息:

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

您也可以在Interface Builder中连接按钮以执行相同的操作。或者混合搭配你的内心。

#1


5  

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

您应该考虑使用代理,通知或某些共享的*实体。

#2


25  

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

#3


1  

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:

为什么不让一个按钮直接将两个消息发送到各自的控制器? UIButton的实例本身就能够向多个目标发送多条消息。要配置按钮,您可以根据需要多次发送以下消息:

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

您也可以在Interface Builder中连接按钮以执行相同的操作。或者混合搭配你的内心。