简体   繁体   中英

How to call an id sender function from another function?

In the following code example, I want to be able to call backgroundTap from another function btnSubmitLoginPassword. What parameter should I pass?

-(IBAction) backgroundTap:(id)sender{

    [userName resignFirstResponder];
    [password resignFirstResponder];

}

-(IBAction) btnSubmitLoginPassword{

    [self backGroundTap:?????????]


    [self validate];


}

PengOne的答案很好,但你问题的确切答案是:传递参数传递nil

[self backgroundTap:nil];

You aren't using sender in the action, so why not just leave it out?

-(IBAction) backgroundTap{
    [userName resignFirstResponder];
    [password resignFirstResponder];
}

-(IBAction) btnSubmitLoginPassword{
    [self backGroundTap];
    [self validate];
}

you are not using id value in the method

    -(IBAction) backgroundTap:(id)sender
   {

     [userName resignFirstResponder];
     [password resignFirstResponder];

    }

instead use like

  -(IBAction) backgroundTap
   {

     [userName resignFirstResponder];
     [password resignFirstResponder];

    }

and then make it call as

  -(IBAction) btnSubmitLoginPassword{

  [self backGroundTap];

   [self validate];
   }

if you want to change/access sender property alone then you should use (id)sender

Usually the sender parameter is UIView or any other object that generates the event. In this case it would be consequent to use self as a sender.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM