ios - 如何从 ActionSheet 更改 UIButton 的文本

标签 ios swift uibutton swift2 uialertaction

我想更改 UIButton city 的文本。

但是它不起作用,你能告诉我这里出了什么问题吗?并且此 IBAction setUpCityDropDown 连接到同一个 UIButton。

 @IBAction func setUpCityDropDown()
        {
            let ActionSheet =  UIAlertController(title: "Which City?", message: "City Name", preferredStyle: UIAlertControllerStyle.ActionSheet)

            let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            }
            let delhiActionActionButton : UIAlertAction = UIAlertAction(title: "Delhi", style: UIAlertActionStyle.Default)
            {   action -> Void in


                self.city.setTitle("Delhi", forState:  UIControlState.Normal)
                self.city.sizeToFit()

            }
            let mumbaiActionActionButton : UIAlertAction = UIAlertAction(title: "Mumbai", style: UIAlertActionStyle.Default)
            {
                action -> Void in
                self.city.setTitle("Mumbai", forState:  UIControlState.Normal)

                self.city.sizeToFit()
            }
            let ahmedabadActionButton : UIAlertAction = UIAlertAction(title: "Ahmedabad", style: UIAlertActionStyle.Default)
            {
                action -> Void in
                self.city.setTitle("Ahmedabad", forState:  UIControlState.Normal)

                self.city.sizeToFit()
            }

            ActionSheet.addAction(cancelActionButton)
            ActionSheet.addAction(ahmedabadActionButton)
            ActionSheet.addAction(delhiActionActionButton)
            ActionSheet.addAction(mumbaiActionActionButton)
            self.presentViewController(ActionSheet, animated: true, completion: nil)
        }

    }

enter image description here

最佳答案

当你在IB中为UIButton设置标题时,它设置的不是String,而是NSAttributedString。因此,您需要使用 setAttributedTitle(_:forState:) 方法来更改它,而不是 setTitle(_:forState:)

@IBAction func setUpCityDropDown()
{
    let ActionSheet =  UIAlertController(title: "Which City?", message: "City Name", preferredStyle: .ActionSheet)
    let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action in }
    let delhiActionActionButton : UIAlertAction = UIAlertAction(title: "Delhi", style: .Default)
    {   
        action in
        self.city.setAttributedTitle(NSAttributedString(string: "Delhi"), forState: .Normal)
        self.city.sizeToFit()
    }
    let mumbaiActionActionButton : UIAlertAction = UIAlertAction(title: "Mumbai", style: .Default)
    {   
        action in
        self.city.setAttributedTitle(NSAttributedString(string: "Mumbai"), forState: .Normal)
        self.city.sizeToFit()
    }
    let ahmedabadActionButton : UIAlertAction = UIAlertAction(title: "Ahmedabad", style: .Default)
    {   
        action in
        self.city.setAttributedTitle(NSAttributedString(string: "Ahmedabad"), forState: .Normal)
        self.city.sizeToFit()
    }
    ActionSheet.addAction(cancelActionButton)
    ActionSheet.addAction(ahmedabadActionButton)
    ActionSheet.addAction(delhiActionActionButton)
    ActionSheet.addAction(mumbaiActionActionButton)
    self.presentViewController(ActionSheet, animated: true, completion: nil)
    }
}

关于ios - 如何从 ActionSheet 更改 UIButton 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665846/

相关文章:

ios - Swift 4 - 带有 AVAudioPlayer 的 UISlider

android - 如何基于 IOS 解决方案解析 Kotlin 中的变量?

objective-c - 模块 'myFramework' 的伞形 header 不包括 header 'otherFramework.h'

swift - 关于如何在按住按钮时隐藏 View

objective-c - 如果标题很长,在 UIButton 的末尾设置点

ios - NSDate 代码未迁移到 Swift 3.0

ios - UITabBarController 的 selectedViewController 未选择正确的 UIViewController

iphone - 如何通过拖动 UIButton 来启动 IBAction?

iphone - box2d 凹体

ios - 从它自己的 UView 子类中添加 UIView 作为 SuperView subview