2

我开始测试ECSlidingViewController并且在我尝试访问之后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解雇。

提前致谢。

4

3 回答 3

2

朋友,您犯了一个小错误,当时您从FirstTopViewControllerUnderRightViewController 导航时需要在 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];
于 2013-10-21T10:15:28.883 回答
1

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

FirstTopViewController *ftvc = //... where ever you get a reference to this from
UnderRightViewController *urvc = ...;
urvc.delegate = ftvc;
于 2013-10-17T03:29:11.480 回答
1

在上面的代码中,您使用的是自定义委托,并且您还使用它来将消息发送到一个控制器类到另一个控制器类。所以下面是自定义委托的相同示例代码,它以您必须实现的类似方式工作正常,而且您的代码中的问题是您没有设置委托,所以请按照下面如何设置相同并调用该方法. 在这里,我使用了与您相同的方法,仅返回我定义为NSString的类型,尽管void用于解释目的,但您可以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在此处输入图像描述

在上面相同的窗口中按下按钮,当按下 Awindow 按钮时,数据将发送
并在 Bwindow 中接收通知,使用上面定义的自定义委托,如屏幕截图中所附。 在此处输入图像描述

于 2013-10-17T14:09:56.697 回答