所以我有3個視圖,如下所示:viewController >> viewController2 >> viewController3。 在viewController3中,我創建了一個委託協議。協議方法是一個打印出NSLog的簡單方法。具有三個viewControllers的代表
當我從ViewController3調用委託時,只有它的父級(viewController2)不響應(第一個)viewController。沒有錯誤。我認爲問題與[v2 setDelegate:self]有關;在viewController.m文件中。不過,[self.v3 setDelegate:self];在ViewController2.m文件中工作正常。
爲什麼(第一個)viewController委託沒有響應?代表只與直接的孩子一起工作嗎?
> **ViewController.h**
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#import "ViewController2.h"
@interface ViewController : UIViewController <PassData>{
ViewController2 *v2;
}
@property (strong, nonatomic) ViewController2 *v2;
> Blockquote
- (IBAction)button:(id)sender;
@end
> **ViewController.M**
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize v2;
- (IBAction)button:(id)sender {
v2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
[v2 setDelegate:self];
[self.view addSubview:v2.view];
}
-(void)print: (BOOL)success;{
if (success == YES) {
NSLog(@"ViewController called");
}
}
@end
> > ViewController2.h
#import <UIKit/UIKit.h>
#import "ViewController3.h"
@interface ViewController2 : UIViewController <PassData> {
ViewController3 *v3;
}
@property (strong, nonatomic)ViewController3 *v3;
@property (retain) id delegate;
- (IBAction)button:(id)sender;
@end
ViewController2.m
#import "ViewController2.h"
@interface ViewController2()
@end
@implementation ViewController2
@synthesize v3,delegate;
- (IBAction)button:(id)sender {
v3 = [[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil];
[self.v3 setDelegate:self];
[self.view addSubview:v3.view];
}
-(void)print: (BOOL)success;{
if (success == YES) {
NSLog(@"ViewController2 called");
}
}
@end
> ViewController3.h
#import <UIKit/UIKit.h>
@protocol PassData <NSObject>
@required
-(void)print:(BOOL)success;
@end
@interface ViewController3 : UIViewController {
id<PassData> delegate;
}
@property (retain) id delegate;
- (IBAction)callButton:(id)sender;
@end
ViewController3.m
#import "ViewController3.h"
@interface ViewController3()
@end
@implementation ViewController3
@synthesize delegate;
- (IBAction)callButton:(id)sender {
// call all delegates
[[self delegate]print:YES];
}
@end
您的問題中的代碼格式不正確(很多空行)並且難以閱讀。首先猜測的是,當您第二次設置委託時,您會覆蓋第一個設置。一位代表伊娃可以只有一個參考。 –
@Andrey - 呃...我認爲你是對的 - setDelegate被覆蓋。如果是這種情況,那麼委託方法如何在同一時間在兩個或更多不同的viewControllers中被調用。我的理解是 - 對於viewController是一個委託,它必須實現的一件事是setDelegate。那有意義嗎 ?? – pete
@pete你問我還是rokjarc? –