Objective-c multiple delegates in the same view -

2019-07-10 07:16发布

I started testing ECSlidingViewController and after I tried to access FirstTopViewController I have a big trouble - because in FirstToViewController I already have ZBarReaderDelegate implemented and all examples of delegate are not triggering any method from my delegate.

Basically I have this stuff:

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);
}

From other side I have

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

I'm trying this entire day solve this puzzle but I never get setTotalViewController fired.

Thanks in advance.

3条回答
The star\"
2楼-- · 2019-07-10 07:33

Friend you did a small mistake, when you navigate from FirstTopViewController to UnderRightViewController at that time you need to do this in 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];
查看更多
Luminary・发光体
3楼-- · 2019-07-10 07:41

You don't have any code that is setting the delegate for the UnderRightViewController. I don't know what object owns both of these controllers, but before either UnderRightViewController and FirstTopViewController are displayed it should run code something like this:

FirstTopViewController *ftvc = //... where ever you get a reference to this from
UnderRightViewController *urvc = ...;
urvc.delegate = ftvc;
查看更多
Viruses.
4楼-- · 2019-07-10 07:50

In your above code you are using custom delegates and also you have used it for sending message to onecontroller class to another controller class. So below is the same sample code of custom delegates, it is working fine in similar way you have to implement and also the problem in your code is you are not setting the delegate, so please follow below how to set the same and call the method. here i have used your same method only return type i have defined as NSString in-spite of void for explaining purpose, but you can use void according to your requirement hope it will be helpful to you:-

First Controller Class 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

Second Controller Class 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

Attached screen shot in Output:- Below is window is the AwindowController.h class enter image description here

Below in the same above window pressing the button and when Awindow button pressed data will send
and notification will be recieved in Bwindow using above define custom delegates as attached in the screen shot. enter image description here

查看更多
登录 后发表回答