【问题标题】:Calling a function from a different view controller for iphone从 iphone 的不同视图控制器调用函数
【发布时间】:2010-03-05 18:22:29
【问题描述】:

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

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

【问题讨论】:

    标签: iphone objective-c function uiviewcontroller call


    【解决方案1】:
    /* 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];
    

    【讨论】:

      【解决方案2】:

      “大家好,我有一个问题,我想从另一个控制器调用一个视图控制器中定义的函数。我尝试了似乎有一百种不同的设置,但似乎没有任何效果。”

      这是因为它是糟糕的设计。控制器不应直接相互交谈。

      您应该考虑使用委托、通知或某些共享中心实体。

      【讨论】:

      • 感谢您的意见。这是一个糟糕的设计吗?也许吧,但问题是,能做到吗?
      • 嘿,St3fan,我回去并再次尝试使用 NsNotificationCenter 并让它工作。我早些时候尝试过,但显然做得不对。我的挫败感让我尝试了非常规的方法 :-) 你的评论让我再次看它,我想通了,所以感谢你的意见。
      【解决方案3】:

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

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

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

      【讨论】:

      • 感谢您提供的信息。从来没有想过尝试,但我会记住它以供将来参考。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多