2

I want to change the text of UIButton city .

But its not working, can you tell me whats the problem here? And this IBAction setUpCityDropDown is connected to same 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

Ketul Patani
  • 101
  • 1
  • 9

1 Answers1

3

When you setup title in IB for UIButton, it setting up not as String, but as NSAttributedString. So you need use setAttributedTitle(_:forState:) method to change it instead of 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)
    }
}
Yury
  • 6,044
  • 3
  • 19
  • 41
  • Its still not working. Does it have anything to do with TouchUpInside ? – Ketul Patani Jun 06 '16 at 20:32
  • Is `city` button outlet connected properly? Try to add `city.setTitle("Test", forState: .Normal)` line in `viewWillAppear` method, will it work? – Yury Jun 06 '16 at 20:37
  • No, no matter what I do the text is not getting changed. – Ketul Patani Jun 06 '16 at 20:41
  • So, we found that the problem is not as described in topic. Its a half of solution at least. Close the topic or add additional question with more code to it. As I think you just forget to set `@IBOutlet weak var city: UIButton!` and connect it to storyboard/xib file – Yury Jun 06 '16 at 20:48
  • No. I have connected to an Outlet. Thats not the problem. And thats the only method I have apart from viewDidLoad. – Ketul Patani Jun 06 '16 at 20:52
  • make breakpoint in `viewDidLoad` or `viewWillAppear` and watch at runtime, if `city` property is nil. Also, add `city.backgroundColor = UIColor.redColor()` to both metods to check if it have any response on screen – Yury Jun 06 '16 at 21:09
  • Haha !! Everything is working, except the setTitle. Not able to set the title in viewDidLoad and viewWillAppear. Though it is not working from viewDidload, but still Is there any problem, because I am changing the text inside UIAlertAction. Because its a different controller ? – Ketul Patani Jun 06 '16 at 21:16
  • I think I found solution. Check my code after 5 min – Yury Jun 06 '16 at 21:23
  • @KetulPatani check it – Yury Jun 06 '16 at 21:31
  • Ya it works. was **NSAttributedString(string: "Mumbai")** , the problem ? – Ketul Patani Jun 06 '16 at 21:36
  • @KetulPatani Yes. I wrote description in head of my answer. – Yury Jun 06 '16 at 21:37