gpt4 book ai didi

iphone - 协议(protocol)方法未被调用,显示委托(delegate) 'nil'

转载 作者:行者123 更新时间:2023-11-29 13:11:31 24 4
gpt4 key购买 nike

我正在开发 iPad 应用程序,但我遇到了委托(delegate)问题...协议(protocol)方法未被调用。我不确定我错过了什么,这是我的代码。

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

最佳答案

我不会从你的代码中得到任何错误,但我建议你在创建 showPickerVC 的对象时清楚地添加它的 delegate self

比如,

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.的更多信息

关于iphone - 协议(protocol)方法未被调用,显示委托(delegate) 'nil',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com