![](/img/trans.png)
[英]How to create a UIButton programmatically in iOS 14 in objective-C?
[英]How can I create one UIButton that allows me to perform 2 IBAction iOS - Objective-C
如何創建一個允許執行2 IBAction
UIButton
。 例如,在用戶按下“關注”之后,按鈕變為“關注”,反之亦然。 用2個按鈕或1個按鈕並使用不同的狀態來完成此操作最好嗎? 我已經研究了如何通過一個按鈕完成此操作,但是還沒有找到任何解決方案。 謝謝!
您可以使用一個按鈕來實現。 您需要將按鈕的標題設置為每個按鈕的狀態,並在發生選擇或取消選擇時在IBAction
方法中切換狀態:
[yourButton setTitle:@"Follow" forState:UIControlStateNormal];
[yourButton setTitle:@"Unfollow" forState:UIControlStateSelected];
並實現IBAction方法,例如:
- (IBAction)btnClicked:(UIButton *)sender
{
sender.selected = !sender.selected;
if (sender.selected)
{
// Currently selected the button and it's title is changed to unfollow
// Do your selection action here
}
else
{
// Currently de-selected the button and it's title is changed to follow
// Do your selection action here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.