2014-12-10 22 views
10

模擬Swift iOS8中的按鈕按下如何在Swift中模擬View中的按鈕按下。我試圖讓日曆權限,並具有這樣的功能設置,可以檢查此授權,當我點擊開始按鈕,這個目前運行如何使用代碼

@IBAction func start(sender: AnyObject) { 
//loads prompt to "Allow" or "Don't Allow" calendar permissions 
eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: handler) 

//checkAuth function looks at calendar authorization to get current status 
and then acts accordingly to dismiss View or load Privacy Settings 
checkAuth() 

} 

我需要能夠模擬開始按鈕的按下一個按鈕來觸發再次啓動()func。到目前爲止,我發現的唯一的事情似乎是在Objective-C

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

我不熟悉的OBJ-C和需要的路要走,如果能夠實現這斯威夫特...謝謝

回答

19

的雨燕翻譯:

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside]; 

是:

button.sendActions(for: .touchUpInside) 

一nd它實際上應該模擬按鈕按下。

+0

這工作完全!!!! ...謝謝 – 2016-09-18 16:24:11

1

嘗試調用您的IBAction爲功能:

self.start(self) 
10

斯威夫特3

button.sendActions(for: UIControlEvents.touchUpInside)

斯威夫特4

button.sendActions(for: .touchUpInside)