Tôi đã triển khai giao thức với phương thức tùy chọn và trong phương thức gọi mà tôi muốn gửi respondsToSelector:
tới self.delegate
trước khi tôi gửi thư nhưng không biên dịch. Thông báo lỗi là:self.delegate respondsToSelector: ... không biên dịch
Không có phương pháp thể hiện đã biết nào cho bộ chọn 'respondsToSelector'.
Là một công việc xung quanh, tôi "vệ sinh" các đại biểu như hình dưới đây, trong đó biên dịch ...
//MyClass.h:
@class MyClass;
@Protocol MyClassDelegate
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
@end
@interface MyClass : UIViewController
@property (nonatomic, weak) id<MyClassDelegate> delegate;
@end
và
//MyClass.m:
...
@synthesize delegate = _delegate;
...
id sanitizedDelegate = self.delegate; //Hmmmm... why does this work?
if ([sanitizedDelegate respondsToSelector:@selector(myClass:willDoSomething:)]) {
[self.delegate myClass:self willDoSomething:animated];
}
.
Tôi đã kiểm tra một số bài đăng bao gồm this one nhưng nó không trả lời sự cố biên dịch thất bại.
Ngoài ra, bộ truy xuất thay thế không làm việc ...
[self delegate]
//or
_delegate
Có ai thấy điều này hoặc có thể tư vấn một cách tốt hơn xử lý?
IOS 5.0: (9A334), Xcode 4.2.1 (4D502)
Ka-ching ... Tôi sẽ mở cửa số 3. – vmanjz
Khai báo đại biểu là NSObject đã làm việc, cảm ơn bạn! – c0d3Junk13