[英]Objective-c multiple delegates in the same view - ECSlidingViewController
我開始測試ECSlidingViewController ,在嘗試訪問FirstTopViewController
之后,我遇到了很大的麻煩-因為在FirstToViewController
我已經實現了ZBarReaderDelegate,並且委托的所有示例都不會從委托中觸發任何方法。
基本上我有這個東西:
#import ...MyStuff...
#import "UnderRightViewController.h"
@interface FirstTopViewController : UIViewController <RightViewDelegate, ZBarReaderDelegate>
@property (weak, nonatomic) IBOutlet UITextView *labelTotal;
@end
#import "FirstTopViewController.h"
@implementation FirstTopViewController
- (void)setTotalViewController:(UnderRightViewController*)controller didTotalChange:(NSString*)total
{
//labelTotal.text = total;
NSLog(@"I'm here!!! and received %@", total);
}
從另一面我有
#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
#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;
在上面的代碼中,您正在使用自定義委托,還使用它來將消息發送到一個控制器類到另一個控制器類。 因此,以下是相同的自定義委托示例代碼,它以與您必須實現的方式相似的方式很好地工作,並且代碼中的問題是您未設置委托,因此請按照以下方法進行設置並調用方法。 在這里,我使用了與您相同的方法,只是返回類型我已定義為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中接收通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.