In the custom delegate implementation, check whether the delegate has implemented the @ protocol method to respond.
Source: Internet
Author: User
In the custom delegate implementation, check whether the delegate has implemented the @ protocol method to respond.
In the custom delegate implementation, we will determine whether the delegate is empty. If not, we will call the corresponding delegate function:
Example: if (delegate _! = Nil)
{
[Delegate _ View: self touchesBegan: touches withEvent: event];
} The above implementation is to determine whether delegate is null. If not, call View: touchesBegan: withEvent: Method of protocol.
However, when a class implements protocol and delegate _ is set as the class object, the class does not implement View: touchesBegan: withEvent: when the method (this method is set to option by protocol), crash will occur.
I thought there was no problem when the option parameter of protocol was set. In fact, it was necessary to determine whether the delegate implemented the protocol method:
The correct code is as follows: if (delegate _! = Nil)
{
If ([(NSObject *) delegate _ respondsToSelector: @ selector (foolTapView: touchesBegan: withEvent:)] = YES)
{
[Delegate _ View: self touchesBegan: touches withEvent: event];
}
}
RespondsToSelector is an NSObject method, so the delegate must be forcibly converted to NSObject
*.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.