Objective-c在同一个视图中的多个委托 – ECSlidingViewController

我开始testingECSlidingViewController,并试图访问FirstTopViewController我有一个很大的麻烦 – 因为在FirstToViewController我已经有ZBarReaderDelegate实现,委托的所有示例都不会触发从我的代理的任何方法。

基本上我有这个东西:

FirstTopViewController.h

 #import ...MyStuff... #import "UnderRightViewController.h" @interface FirstTopViewController : UIViewController <RightViewDelegate, ZBarReaderDelegate> @property (weak, nonatomic) IBOutlet UITextView *labelTotal; @end 

FirstTopViewController.m

 #import "FirstTopViewController.h" @implementation FirstTopViewController - (void)setTotalViewController:(UnderRightViewController*)controller didTotalChange:(NSString*)total { //labelTotal.text = total; NSLog(@"I'm here!!! and received %@", total); } 

从我的另一边

UnderRightViewController.h

 #import <UIKit/UIKit.h> #import "ECSlidingViewController.h" @class UnderRightViewController; @protocol RightViewDelegate <NSObject> - (void)setTotalViewController:(UnderRightViewController*)controller didTotalChange:(NSString*)total; @end @interface UnderRightViewController : UITableViewController @property (nonatomic, weak) id <RightViewDelegate> delegate; @end 

UnderRightViewController.m

 #import "UnderRightViewController.h" @interface UnderRightViewController () @end @implementation UnderRightViewController @synthesize delegate; - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [delegate setTotalViewController:self didTotalChange:@"foo"]; } @end 

我试图整天解决这个难题,但我从来没有得到setTotalViewController解雇。

提前致谢。

朋友你犯了一个小错误,当你从FirstTopViewController导航到UnderRightViewController的时候,你需要在FirstTopViewController.m中做到这一点:

  UnderRightViewController *obj = [[UnderRightViewController alloc] initWithNibName:@"UnderRightViewController" bundle:nil]; obj.delegate = self; // u forget to assign protocol handler [self.navigationController pushViewController:obj animated:YES]; [obj release]; 

您没有设置UnderRightViewController的委托的任何代码。 我不知道哪个对象拥有这两个控制器,但是在显示UnderRightViewController和FirstTopViewController之前,它应该运行如下所示的代码:

 FirstTopViewController *ftvc = //... where ever you get a reference to this from UnderRightViewController *urvc = ...; urvc.delegate = ftvc; 

在你的上面的代码中,你正在使用自定义委托,并且你已经使用它将消息发送到一个控制器类到另一个控制器类。 所以下面是自定义代表的相同的示例代码,它是以类似的方式工作正常,你必须执行,而且你的代码中的问题是你没有设置委托,所以请按照下面如何设置相同的方法和调用方法。 在这里我已经使用了你的方法只返回types我已经定义为NSString尽pipevoid的解释目的,但你可以根据你的要求使用void希望它会对你有所帮助: –

第一个控制器类AWindowController.h

  @interface AWindowController : NSWindowController<sampleDelegate> { NSString *textA; } @property(readwrite,retain)NSString *textA; -(IBAction)doSet:(id)sender; @end #import "AWindowController.h" #import "BWindowController.h" @interface AWindowController () @end @implementation AWindowController @synthesize textA; - (id)initWithWindow:(NSWindow *)window { self = [super initWithWindow:window]; if (self) { // Initialization code here. } return self; } - (NSString *)setTotalViewController:(BWindowController*)controller didTotalChange:(NSString*)total { NSLog(@"recieved"); return @"recieved"; } - (void)windowDidLoad { [super windowDidLoad]; // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. } -(NSString*)windowNibName { return @"AWindowController"; } -(IBAction)doSet:(id)sender { [self setTextA:@"Awindow Button Pressed"]; BWindowController *b=[[BWindowController alloc]init]; b.delegate=self; [b showWindow:self]; } @end 

第二个控制器类BWindowController.h

  #import <Cocoa/Cocoa.h> #import "sampleDelegate.h" @class BWindowController; @protocol sampleDelegate <NSObject> @required //-(NSString *)getDataValue; - (NSString *)setTotalViewController:(BWindowController*)controller didTotalChange:(NSString*)total; @end @interface BWindowController : NSWindowController<sampleDelegate> { NSString *bTextValue; id<sampleDelegate>delegate; } @property(readwrite,retain)NSString *bTextValue; @property(readwrite,assign)id<sampleDelegate>delegate; @end #import "BWindowController.h" @interface BWindowController () @end @implementation BWindowController @synthesize bTextValue,delegate; - (id)initWithWindow:(NSWindow *)window { self = [super initWithWindow:window]; if (self) { // Initialization code here. } return self; } - (NSString *)setTotalViewController:(BWindowController*)controller didTotalChange:(NSString*)total; { return nil; } - (void)windowDidLoad { NSString *str= [[self delegate]setTotalViewController:self didTotalChange:@"recieved"]; self.bTextValue=str; [super windowDidLoad]; // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. } -(NSString*)windowNibName { return @"BWindowController"; } @end 

输出中的附加屏幕截图: – 下面的窗口是AwindowController.h在这里输入图像说明

在同一个窗口下面按下button,当Awindowbutton按下数据时将发送
并在Bwindow中使用上面定义的自定义代表作为附件在屏幕截图中收到通知。 在这里输入图像说明