1

我正在开发一个 iPad 应用程序,但我遇到了委托问题……协议方法没有被调用。我不确定我错过了什么,这是我的代码。

@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;
}
4

1 回答 1

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

}

有关如何创建/使用协议的更多信息。

于 2013-06-22T05:12:19.883 回答