从iPhone的不同视图控制器调用函数 [英] Calling a function from a different view controller for iphone

查看:143
本文介绍了从iPhone的不同视图控制器调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想从另一个控制器调用一个视图控制器中定义的函数。
我尝试了一百种不同的设置,似乎没有任何工作。

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.

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

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


推荐答案

Hi所有的问题,我都想从另一个控制器调用一个视图控制器中定义的函数,我尝试了一百个不同的设置,看起来没有任何效果。

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

这篇关于从iPhone的不同视图控制器调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆