Ios 如何以编程方式更改UIButton标签
当我第一次运行我的应用程序时,我从我的服务器检索一个数字,并将其显示在我的UIButton标签上。将其视为显示在红色按钮上的通知编号 当我删除应用程序中的通知时,我希望我的UIButton标签减少1。删除通知后,我可以从服务器获取递减的数字,但无法在UIButton上显示此新数字。该按钮始终显示首次启动应用程序时的号码 删除更新UIButton的通知后,我调用Ios 如何以编程方式更改UIButton标签,ios,xcode,swift,uibutton,Ios,Xcode,Swift,Uibutton,当我第一次运行我的应用程序时,我从我的服务器检索一个数字,并将其显示在我的UIButton标签上。将其视为显示在红色按钮上的通知编号 当我删除应用程序中的通知时,我希望我的UIButton标签减少1。删除通知后,我可以从服务器获取递减的数字,但无法在UIButton上显示此新数字。该按钮始终显示首次启动应用程序时的号码 删除更新UIButton的通知后,我调用makeButtonView()方法 func makeButtonView(){ var button = makeButton
makeButtonView()
方法
func makeButtonView(){
var button = makeButton()
view.addSubView(button)
button.tag = 2
if (view.viewWithTag(2) != nil) {
view.viewWithTag(2)?.removeFromSuperview()
var updatedButton = makeButton()
view.addSubview(updatedButton)
}else{
println("No button found with tag 2")
}
}
func makeButton() -> UIButton{
let button = UIButton(frame: CGRectMake(50, 5, 60, 40))
button.setBackgroundImage(UIImage(named: "redBubbleButton"), forState: .Normal)
API.getNotificationCount(userID) {
data, error in
button.setTitle("\(data)", forState: UIControlState.Normal)
}
button.addTarget(self, action: "targetController:", forControlEvents: UIControlEvents.TouchUpInside)
return button
}
这相当简单
import UIKit
class ViewController: UIViewController {
@IBOutlet var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.setTitle("hello world", forState: UIControlState.Normal)
}
}
我相信,如果将状态设置为“正常”,则默认情况下,该值将传播到其他状态,只要您没有明确为这些状态设置标题
换言之,如果将其设置为“正常”,则当按钮进入其他状态时,也应显示此标题
UIControlState.allZeros
UIControlState.Application
UIControlState.Disabled
UIControlState.Highlighted
UIControlState.Reserved
UIControlState.Selected
最后,以防您有其他问题。我需要更多信息来为您提供正确的代码。但这种方法应该奏效:
lazy var button : UIButton = {
let button = UIButton(frame: CGRectMake(50, 5, 60, 40))
button.setBackgroundImage(UIImage(named: "redBubbleButton"), forState: .Normal)
button.addTarget(self, action: "targetController:", forControlEvents: UIControlEvents.TouchUpInside)
return button
}()
func makeButtonView(){
// This should be called just once!!
// Likely you should call this method from viewDidLoad()
self.view.addSubview(button)
}
func updateButton(){
API.getNotificationCount(userID) {
data, error in
// be sure this is call in the main thread!!
button.setTitle("\(data)", forState: UIControlState.Normal)
}
}
自Swift 4以来,已有一些更新。这对我很有用: self.button.setTitle(“按钮标题”,用于:UIControl.State.init(原始值:0))
将按钮替换为您的IBOutlet名称。您还可以使用变量或数组代替引用的文本 对Swift 4或5使用此代码
button.setTitle("Click Me", for: .normal)
由于API调用应该在后台线程上运行,因此需要将UI更新发送回主线程,如下所示:
DispatchQueue.main.async {
button.setTitle(“new value”, forState: .normal)
}
设置标题后,只需简单地重新绘制按钮即可:
button.setNeedsDisplay();
我不认为在视图中添加/删除按钮并创建两次按钮有什么意义。不管怎样,updatedNotificationButton是什么?var updatedButton=makeButton()view.addSubview(updatedNotificationButton)我这样做是因为标签没有重置为新的数字。总是同一个号码。我想我需要关闭按钮并重新创建它,但这也没用。我修复了您指出的代码。我正在以编程方式创建按钮,为什么我需要一个插座?按钮不会重置标题。顺便说一句,我看不出你的代码在设置标题时与我的代码有什么不同。它不起作用。它就像以前一样工作。按钮标签号没有减少。但是我可以看到删除通知后从服务器返回的号码。只是让您知道我在另一个页面中删除了通知。然后我关闭通知页面。按钮和通知不在同一情节提要上。情节提要?对不起,那没有任何意义。您是使用故事板还是以编程方式创建按钮?红色通知按钮和通知视图(我在其中删除通知)是同一应用程序的两个不同部分。当我第一次运行应用程序时,点击通知按钮,就会出现通知视图。我删除一个通知,关闭页面,我看到红色按钮标签没有改变。是的,我正在通过编程创建按钮。对不起,我应该说故事板,它们不在同一视图上。