如何使用 Swift 在 iOS 中更改 UIButton 的状态? [英] How to change the state of an UIButton in iOS using Swift?

查看:128
本文介绍了如何使用 Swift 在 iOS 中更改 UIButton 的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有六个按钮的 Objective-C 工作代码,但我似乎没有找到如何使用 Swift 更改按钮的状态.那么如何让下面的代码在 Swift 中工作呢?

I have Objective-C working code for six buttons but I don't seem to find how to change the state of a button using Swift. So how do I make the following code work in Swift?

[self.button2 setBackgroundImage:[UIImage imageNamed:@"button2_selected.png"] forState: UIControlStateSelected];
button1.selected = NO;
button2.selected = !button2.selected;
button3.selected = NO;
button4.selected = NO;
button5.selected = NO;
button6.selected = NO;

推荐答案

检查:

    var var_button_ =   UIButton()
    var_button_.setBackgroundImage(UIImage(named: "button2_selected.png"), forState: UIControlState.Normal)
    var_button_.selected = false
    var_button_.selected = !var_button_.selected
    //var_button_.selected = true

谢谢作者:JNYJ

这篇关于如何使用 Swift 在 iOS 中更改 UIButton 的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆