我想更改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)
}
}
答案 0 :(得分:3)
当您在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)
}
}