简体   繁体   English

如何在某些情况下更改以编程方式创建的UIButton的标题和操作

[英]How to change the title and action of a programmatically created UIButton on some condition

I have created a UIButton programmatically on my ViewController. 我已经在ViewController上以编程方式创建了一个UIButton I want to perform different actions on the same button depending on the condition and want to change the title as well. 我想根据条件在同一个按钮上执行不同的操作,并希望更改标题。

First I create the button like this: 首先,我创建这样的按钮:

func createButton(buttonTitle: String,buttonAction: Selector) -> UIButton{
    let button   = UIButton(type: UIButtonType.System) as UIButton
    button.frame = CGRectMake(0, 0, 414, 65)

    button.setTitle(buttonTitle, forState: UIControlState.Normal)
    button.addTarget(self, action:buttonAction, forControlEvents: UIControlEvents.TouchUpInside)
    button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
    button.titleLabel?.font = UIFont(name: Variables.MONTESERRAT_REGULAR, size: 20.0)

    button.backgroundColor = UIColor().blueColor()       //top
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0,10.0, 10.0, 0.0)
      return button
}

Then I am showing like this 然后我像这样显示

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height))

    if(active == true){
       bottomButton = createButton("UNPUBLISH", buttonAction: "unPublishToServer")
    }else if(active == false){
        bottomButton = createButton("PUBLISH", buttonAction: "publishToServer")
    }else{
        bottomButton = createButton("Request", buttonAction: "requestItem")
    }

    footerView.addSubview(bottomButton!)
    return footerView
}

then on certain messages from server or conditions I am changing the button like this 然后在来自服务器或条件的某些消息上,我正在像这样更改按钮

func publishTripToServer(){
    dispatch_async(dispatch_get_main_queue()) {
        self.bottomButton?.setTitle("UNPUBLISH", forState: UIControlState.Normal)
    }
}

func unPublishTripToServer(){
    dispatch_async(dispatch_get_main_queue()) {
        self.bottomButton?.setTitle("PUBLISH", forState: UIControlState.Normal)
    }
}

The problem I am having is first it shows some background color behind the title when I click publish or unpublish. 我遇到的问题是,当我单击“发布”或“取消发布”时,它首先在标题后面显示了一些背景色。 the second issue is button is not changing the action. 第二个问题是按钮未更改操作。

I'm not exactly sure what you mean for the background color issue. 我不确定您对背景色问题的含义。

But for your button, does something like this not work? 但是对于您的按钮,这样的操作不起作用吗?

func publishTripToServer(){

    self.bottomButton = createButton("UNPUBLISH", buttonAction: "unPublishToServer")
}



 func unPublishTripToServer(){
     self.bottomButton = createButton("PUBLISH", buttonAction: "publishToServer")
}

I don't know why you previously were trying to update the button title on the background thread, but you shouldn't ever update ui elements asynchronously. 我不知道您为什么以前尝试在后台线程上更新按钮标题,但是您不应该异步更新ui元素。

And the reason your button action wasn't changing is that you never told it to change - you just changed the title 而且您的按钮动作没有改变的原因是您从未告诉过要改变-您只是改变了标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM