簡體   English   中英

在動作中設置按鈕文字

[英]Set button text inside action

我有一個按鈕,我想以編程方式設置文本。

至於現在我有以下內容:

@IBAction func saveButtonClicked(_ sender: AnyObject) {
    sender.setTitle("Action", for: UIControlState.normal)
    closeKeyboard()
}

問題是,這是一個動作輸出,因此文本僅在我按下按鈕時才追加到按鈕。

我試圖將setTitle放在viewdidload函數中,但不會編譯。 我如何使文本恆定? 我必須開設多個網點,還是可以通過其他方式完成?

首先創建一個名為button_1的插座。

@IBOutlet var button_1: UIButton!

在viewDidLoad中添加

button_1.setTitle("Button text", forState: .Normal)

要在單擊按鈕時觸發操作,請使用以下命令:

 @IBAction func ButtonClicked(sender: UIButton){
        button_1.setTitle("You clicked me", forState: .Normal)
    }

您可以將IBOutlets繪制到按鈕上以設置其標題。

btnOutlet.setTitle("yourTitle", forState: .Normal)

另外,您可以從情節提要中將tag設置為按鈕,在viewDidLoad()中對其進行訪問,然后如下設置其標題:

 let btn:UIButton = self.view.viewWithTag(500) as! UIButton
 btn.setTitle("yourTitle", forState: .Normal)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM