programing

Swift Xcode 6의 버튼 텍스트를 변경하는 방법

powerit 2023. 4. 13. 21:14
반응형

Swift Xcode 6의 버튼 텍스트를 변경하는 방법

이게 내가 하려는 일이야.만약 당신이 헤일로나 CoD를 해본 적이 있다면, 당신은 무기 방출의 이름을 바꿀 수 있다는 것을 알 것이다.

텍스트 필드를 사용하여 출력 이름을 변경할 수 있도록 만들고 있습니다.문제는 다음과 같습니다. Loadout 메뉴의 Loadout 이름은 버튼(Loadout에 대한 정보를 선택하고 보기 위한 것)이며, 다음과 같이 쓸 수 있습니다.

@IBAction func renameClassButton(sender: AnyObject) {
    classTopButton.text = "\(classTopTextField)"
}

단, [classTopButton]는 '.text' 접미사를 사용할 수 없는 버튼입니다.

다음 작업을 수행할 수 있습니다.

button.setTitle("my text here", forState: .normal)

Swift 3, 4, 5:

button.setTitle("my text here", for: .normal)

Xcode 8 - Swift 3의 경우:

button.setTitle( "entertext" , for: .normal )

이제 스위프트3를 위해

    let button = (sender as AnyObject)
    button.setTitle("Your text", for: .normal)

(변수의 계속적인 선언은 필요 없습니다.버튼의 송신자를 다음과 같이 사용해 주세요.

    (sender as AnyObject).setTitle("Your text", for: .normal)

이것은 버튼의 IBAtion 내에서 사용됩니다.

주의:

someButton.setTitle("New Title", forState: .normal)

제목 유형이 일반인 경우에만 작동합니다.

여기에 이미지 설명 입력

3개뿐만 아니라 4개의 작업도 신속합니다.

libero.setTitle("---", for: .normal)

여기서 libero는 의부톤이다.

sender 인수를 사용할 수 있습니다.

  @IBAction func TickToeButtonClick(sender: AnyObject) {

        sender.setTitle("my text here", forState: .normal)


}

Swift 4에서는 이전에 이 모든 것을 시도했지만, 다음과 같이만 실행됩니다.

@IBAction func myButton(sender: AnyObject) {
   sender.setTitle("This is example text one", for:[])
   sender.setTitle("This is example text two", for: .normal)
}

NSButton을 사용하는 경우,setTitle펑크가 아니라 재산이에요

@IBOutlet weak var classToButton: NSButton!

. . .


classToButton.title = "Some Text"

언급URL : https://stackoverflow.com/questions/26641571/how-to-change-button-text-in-swift-xcode-6

반응형