Cambio de texto de UIButton mediante programación swift
Simple pregunta aquí. Tengo un UIButton, currencySelector, y quiero cambiar el texto programáticamente. Esto es lo que tengo:
currencySelector.text = "foobar"
Xcode me da el error "Declaración esperada". ¿Qué estoy haciendo mal y cómo puedo hacer que el texto del botón cambie?
7 answers
En Swift 3:
button.setTitle("Button Title",for: .normal)
De lo contrario:
button.setTitle("Button Title", forState: UIControlState.Normal)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-26 00:29:40
Solo una aclaración para los nuevos en programación Swifty iOS. Debajo de la línea de código:
button.setTitle("myTitle", forState: UIControlState.Normal)
Solo se aplica a IBOutlets
, no a IBActions
.
Por lo tanto, si su aplicación está utilizando un botón como función para ejecutar algún código, por ejemplo, reproducir música, y desea cambiar el título de Play
a Pause
basado en una variable de conmutación, también debe crear un IBOutlet
para ese botón.
Si intentas usar button.setTitle
contra un IBAction
obtendrás un error. Es obvio una vez que lo sé, pero para los noobs (todos lo éramos) este es un consejo útil.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-10 21:04:41
Swift 3:
Establecer el título del botón:
//for normal state:
my_btn.setTitle("Button Title", for: .normal)
// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-23 12:30:26
Swift 3.0
// Standard State
myButton.setTitle("Title", for: .normal)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-24 13:27:59
Swift 4:
for state: UIControlState in [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved] {
button.setTitle(NSLocalizedString("Title", comment: ""), for: state)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-27 05:13:43
Swift 3
Cuando haces la @ IBAction:
@IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("string goes here", for: .normal)
}
Esto establece el remitente como UIButton (en lugar de Cualquier) por lo que apunta a la btnAction como un UIButton
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-21 23:35:56
Swift 3
let button: UIButton = UIButton()
button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
button.setTitle(“Title Button”, for: .normal)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-12 16:29:35