2011-06-16 69 views
6

在下面的代碼示例中,我希望能夠從另一個函數btnSubmitLoginPassword調用backgroundTap。我應該傳遞什麼參數?如何從另一個函數調用一個id發件人函數?

-(IBAction) backgroundTap:(id)sender{ 

    [userName resignFirstResponder]; 
    [password resignFirstResponder]; 

} 

-(IBAction) btnSubmitLoginPassword{ 

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


    [self validate]; 


} 

回答

14

PengOne的回答是不錯的,但具體回答你的問題是:通過nil發件人參數:

[self backgroundTap:nil]; 
+0

+1:我沒有回答問題,我解決了它。你回答了,並且正確。 – PengOne 2011-06-16 05:44:04

+0

我更喜歡用這種方式,因爲它可以讓我稍後改變它更容易 – petershine 2011-06-16 05:52:04

5

您在操作中沒有使用sender,那麼爲什麼不直接放棄它呢?

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

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

你是不是在方法中使用的值id

-(IBAction) backgroundTap:(id)sender 
    { 

    [userName resignFirstResponder]; 
    [password resignFirstResponder]; 

    } 

,而不是使用像

-(IBAction) backgroundTap 
    { 

    [userName resignFirstResponder]; 
    [password resignFirstResponder]; 

    } 

,然後作出稱其爲

-(IBAction) btnSubmitLoginPassword{ 

    [self backGroundTap]; 

    [self validate]; 
    } 

,如果你想更改/訪問發件人屬性,然後你笑ULD使用(ID)發送

+2

看起來很熟:http://stackoverflow.com/questions/6367468/how-to-call-an-id-sender-function-from-another-function/6367493#6367493 – PengOne 2011-06-16 05:39:05

2

通常發送參數的UIView或產生任何其他對象事件。在這種情況下,將使用自己作爲發件人。

相關問題