如何以编程方式更改按钮的标题
How to change a button's title programmatically
我有一个 Master-DetailView 项目,我需要通过数组更改按钮的标题,就像我在这里所做的那样:
import UIKit
import AVFoundation
import MapKit
class TemplateTestViewController: UIViewController,MKMapViewDelegate {
@IBOutlet var ProfileMapView: MKMapView!
@IBOutlet var BarOrClubLabel: UILabel!
@IBOutlet var HeaderProfileName: UILabel!
@IBOutlet var HeaderprofileImage: UIImageView!
@IBOutlet var StreetProfileLabel: UILabel!
@IBOutlet var ProfilePhoneNumberLabel: UIButton!
@IBOutlet var WorkTimeLabel: UILabel!
@IBOutlet var AgeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Set Ups the Image Template :
HeaderprofileImage.image = UIImage(named: ProfileImages[MyIndex])
//Sets Up The BIG Name Template :
HeaderProfileName.text = ProfileNames[MyIndex]
//Sets Up The Navigation Bar Name :
navigationItem.title = ProfileNames[MyIndex]
//Sets Up Street Text :
StreetProfileLabel.text = ProfileStreets[MyIndex]
//Sets Up Bar Or Club Text :
BarOrClubLabel.text = ProfileBarOrClub[MyIndex]
//Sets Up Age Text :
AgeLabel.text = AgeText[MyIndex]
我试过了:
ProfilePhoneNumberLabel.setTitle(ProfilePhoneNumbers[MyIndex], for: UIControlState.normal)
它没有改变。
有没有办法做这样的事情?如果可以,有人可以告诉我怎么做吗?
先谢谢了
ProfilePhoneNumberLabel
的标题设置方法如下
ProfilePhoneNumberLabel.setTitle(PhoneNumbers[MyIndex], for: .normal)
for
参数允许您设置按钮在各种状态下的文本。在大多数情况下,为 normal
状态设置标题应该就足够了。
嗯......这很尴尬,但你必须在情节提要中将你的 UIButton 标题设置为 Plain 才能更改它
ProfilePhoneNumberLabel.setTitle(PhoneNumbers[MyIndex], for: .normal)
+
普通UIButton
标题
=
快乐露营者
我有一个 Master-DetailView 项目,我需要通过数组更改按钮的标题,就像我在这里所做的那样:
import UIKit
import AVFoundation
import MapKit
class TemplateTestViewController: UIViewController,MKMapViewDelegate {
@IBOutlet var ProfileMapView: MKMapView!
@IBOutlet var BarOrClubLabel: UILabel!
@IBOutlet var HeaderProfileName: UILabel!
@IBOutlet var HeaderprofileImage: UIImageView!
@IBOutlet var StreetProfileLabel: UILabel!
@IBOutlet var ProfilePhoneNumberLabel: UIButton!
@IBOutlet var WorkTimeLabel: UILabel!
@IBOutlet var AgeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Set Ups the Image Template :
HeaderprofileImage.image = UIImage(named: ProfileImages[MyIndex])
//Sets Up The BIG Name Template :
HeaderProfileName.text = ProfileNames[MyIndex]
//Sets Up The Navigation Bar Name :
navigationItem.title = ProfileNames[MyIndex]
//Sets Up Street Text :
StreetProfileLabel.text = ProfileStreets[MyIndex]
//Sets Up Bar Or Club Text :
BarOrClubLabel.text = ProfileBarOrClub[MyIndex]
//Sets Up Age Text :
AgeLabel.text = AgeText[MyIndex]
我试过了:
ProfilePhoneNumberLabel.setTitle(ProfilePhoneNumbers[MyIndex], for: UIControlState.normal)
它没有改变。
有没有办法做这样的事情?如果可以,有人可以告诉我怎么做吗? 先谢谢了
ProfilePhoneNumberLabel
ProfilePhoneNumberLabel.setTitle(PhoneNumbers[MyIndex], for: .normal)
for
参数允许您设置按钮在各种状态下的文本。在大多数情况下,为 normal
状态设置标题应该就足够了。
嗯......这很尴尬,但你必须在情节提要中将你的 UIButton 标题设置为 Plain 才能更改它
ProfilePhoneNumberLabel.setTitle(PhoneNumbers[MyIndex], for: .normal)
+
普通UIButton
标题
=
快乐露营者