2013-06-22 167 views
1

我正在研究一個iPad應用程序,我遇到了委託問題......協議方法沒有被調用。我不確定我錯過了什麼,這裏是我的代碼。協議方法沒有被調用,顯示委託'nil'

@protocol pickerLabelProtocol <NSObject> 
- (void)selectedPickerData:(UILabel *)sender; 
@end 

@interface showPickerVC : UIViewController 
@property (nonatomic, strong) id <pickerLabelProtocol> delegate; 
@end 

@implementation showPickerVC 
- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
//i used breakpoint, the 'delegate' is always nil for some reason? 
    [self.delegate selectedPickerData:self.mainLabel]; 
} 

---------------------- 
@interface someViewController : UIViewController <pickerLabelProtocol> 
@property (nonatomic, strong) showPickerVC *showPicker; 
@end 

@implementation someViewController 

- (void)selectedPickerData:(UILabel *)sender 
{ 
//protocol method 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.showPicker = [[showPickerVC alloc]init]; 
    self.showPicker.delegate = self; 
} 
+0

究竟是你想要做什麼?你想要訪問什麼? – Durgaprasad

+0

「ShowPickerVC」是popOver,來自「someViewController」...「ShowPickerVC」有一個選擇器,用戶在選擇器中選擇數據,我希望選定的數據被傳遞給「someViewController」... – DevCali

+0

我是不知道爲什麼,但這段代碼在iPhone中正常工作,但不與iPad可能是因爲popOver? – DevCali

回答

1

我不能有任何錯誤,從你的代碼,但我建議你要清楚,當你創建的showPickerVC對象添加它delegate

諸如此類有關,

showPickerVC *obj = [[showPickerVC alloc] init]; 
obj.delegate = self; /// YOur protocol delegate 
. 
. 
[self presentModalViewController:obj animated:YES]; 

並且下面添加代碼

@implementation showPickerVC 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    if([self.delegate respondsToSelector:@selector(selectedPickerData:)]) 
    { 
     [self.delegate selectedPickerData:self.mainLabel]; 
    } 

} 

有關How to create/use of Protocol.的詳細信息

+0

感謝您的回覆......問題在於「委託」總是顯示爲零,這是爲什麼它不會調用該方法...是否與popOver有關?此代碼可以正常使用iPhone ... – DevCali