-2

Please explain the meaning of the second if statement.

What i am asking is that meaning of if([self.delegate textFieldShouldClear:self]) <-- This line

 SEL clearselector = @selector(textFieldShouldClear:);

 if([self.delegate respondsToSelector:clearselector]){
     if([self.delegate textFieldShouldClear:self]){// (Explain This If please)?
         NSLog(@"Delegate Methods");
         self.txtRef.text = @"";
     }
 }

Thanks in advance.

  • The method in question is https://developer.apple.com/reference/uikit/uitextfielddelegate/1619594-textfieldshouldclear but I'm not sure what sort of explanation you are looking for. – Jonah Mar 01 '17 at 06:41
  • Possible duplicate of [when to use respondsToSelector in objective-c](http://stackoverflow.com/questions/3697058/when-to-use-respondstoselector-in-objective-c) – Harshal Valanda Mar 01 '17 at 06:41
  • The `If` says `textFieldShouldClear` is set to return YES or, not, if `YES` it will clear the textfield, else no. – iphonic Mar 01 '17 at 06:45
  • Thanks guys for the answers –  Mar 01 '17 at 06:49

2 Answers2

1

Generally this method return BOOL value which indicates that this class has implemented or inherited provided delegated method and by this BOOL value YES or NO we will call it to notify or update data.

Its generally use to prevent from crash if that class has not implemented that method will not crash our app.

In your case your checking UITextField Delegate method is implemented by that class or not if YES than you will call that method through delegate object.

EDIT:

As per your question update textFieldShouldClear method is to check that user has override textFieldShouldClear this method and ask user that textFiled should clear or not.

Apple Source :

Asks the delegate if the text field’s current contents should be removed.

Reference Link : https://developer.apple.com/reference/uikit/uitextfielddelegate/1619594-textfieldshouldclear

Hope this will help to understand this method.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
0

That delegate method return true only then if contain will execute.

{
    NSLog(@"Delegate Methods");
     self.txtRef.text = @"";

}

User may return false if he don't want to clear textfield for some particular case.

sschunara
  • 2,285
  • 22
  • 31