【问题标题】:How to change the text of UIButton from ActionSheet如何从 ActionSheet 更改 UIButton 的文本
【发布时间】:2016-10-06 13:23:42
【问题描述】:

我想更改 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)
        }

    }

【问题讨论】:

    标签: ios swift uibutton swift2 uialertaction


    【解决方案1】:

    当您在 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)
        }
    }
    

    【讨论】:

    • 它仍然无法正常工作。和 TouchUpInside 有关系吗?
    • city 按钮插座是否连接正确?尝试在viewWillAppear 方法中添加city.setTitle("Test", forState: .Normal) 行,可以吗?
    • 不,无论我做什么,文本都不会改变。
    • 所以,我们发现问题与主题描述的不一样。它至少是解决方案的一半。关闭主题或添加带有更多代码的附加问题。因为我认为您只是忘记设置@IBOutlet weak var city: UIButton! 并将其连接到故事板/xib 文件
    • 没有。我已连接到插座。那不是问题。这是我除了 viewDidLoad 之外的唯一方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2016-05-12
    • 1970-01-01
    • 2017-10-15
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多