Duck
Duck

Reputation: 35953

iOS - is this declaration inside a class correct?

Suppose I am creating a subclass of UITextView named for example, myTextView. I will make that class its own delegate. So, I need to implement the delegates methods like

- (void)textViewDidBeginEditing:(UITextView *)textView {

Should I declare this method like that or like this?

- (void)textViewDidBeginEditing:(myTextView *)textView {

it appears to me like recursive, because I am inside myTextView class, defining a delegate that is referring itself...

what is the correct approach? thanks.

Upvotes: 4

Views: 148

Answers (1)

atxe
atxe

Reputation: 5079

Just for clarification is better to implement the methods like are defined in the protocol:

 - (void)textViewDidBeginEditing:(UITextView *)textView

If you implement the protocol methods with your custom class type or whatever class type, the methods will still get called because there is not type check performed. The parameter will be actually your custom subclass. Anyway, and again for clarification, I suggest to have an inner cast if you want to deal with the ivars of your custom subclass:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    MyTextView * myTextView = (MyTextView *)textView;
    ...
}

Upvotes: 4

Related Questions