Change the Text of a Label with a Button in iOS

Change the text of a label with a button in iOS

change your code as per below

var txt = "Hey"

@IBOutlet weak var Text: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
showText()
}

func showText(){
Text.text = txt
}

@IBAction func Button(_ sender: UIButton) {
txt = "Hello"
showText()
}

Change label text of tableView on button click Swift

Add to the ViewController:

private var areNamesVisible = false

@IBAction func didPressButton(_ sender: Any) {
areNamesVisible.toggle()
tableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = names[indexPath.row]
cell.textLabel?.text = areNamesVisible ? name : "cell \(indexPath.row + 1)"
return cell
}

How to update a label text with button inside a collection view in swift 4

If I understood correctly, each time you push + or - you want to update slot label. In my opinion the easiest and fastest way to achieve this it's using NotificationCenter.default.post

In your collection view cell on button action write:

NotificationCenter.default.post(name: Notification.Name("postAction"), object: numberToIncreaseOrDecrease)

In your MainViewController where you have the slot label, add this code in view did load:

NotificationCenter.default.addObserver(self, selector: #selector(updateSlotValue(_:)), name: NSNotification.Name("postAction"), object: nil)

And out from the view did load add this function:

 @objc func updateSlotValue(_ notification: Notification) {
let value = notification.object as! Int
cartSlot_lbl.text.text = value
}

Change Button label/text when pushed

You can use this:

@IBAction func button(_ sender: Any) {
(sender as! UIButton).setTitle("newTitle", for: [])
}

Or just:

@IBAction func buttonTapped(_ sender: UIButton) {
sender.setTitle("newTitle", for: [])
}

How to change label text of a label in a popover view controller when pressing a button in the main view?

As mentioned in comments, you seem to be instantiating the popup controller 2 times, so try it like this in your showPopOver code:

 let myViewController = storyboard?.instantiateViewController(withIdentifier:   "popupController") as! PopUpVC

myViewController.preferredContentSize = CGSize(width: 350, height: 200)
myViewController.modalPresentationStyle = .popover

myViewController.tipText = '' //set to what ever

Dynamically change label when pressing button - iOS Swift

In order to change the label, you need a reference to it. You need to make an @IBOutlet and then change the label through that.

I see that you have @IBActions already, so maybe you know that process already. Open your storyboard and view controller source side by side in the assistant editor. Then select the label. Switch to the connections inspector and then drag from the small circle next to "New Referencing Outlet" and into the view controller source file. This should create the @IBOutlet for you.

Then it is just a matter of changing the label, e.g.

@IBOutlet weak var MyLabel: UILabel!

@IBAction func subtractionFunctionButtonPressed(sender: UIButton) {
//Subtracting Function
MyLabel.text = "-"
}

Updating Label Text several times while a Button is pressed in Swift

You can run all of your long-running tasks in a background queue, and make the label updates in the main queue. The key is to call the next function only when the first is finished, or they will run in parallel, and could all update at the same time. Here's an example

func doSomething1() {
// as good a way as any to simulate a long-running process
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text1"
}
DispatchQueue.global(qos: .background).async {
self.doSomething2()
}
}
func doSomething2() {
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text2"
}
DispatchQueue.global(qos: .background).async {
self.doSomething3()
}
}
func doSomething3() {
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text3"
}
DispatchQueue.global(qos: .background).async {
self.doSomething4()
}
}
func doSomething4() {
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text4"
}
}

@IBAction func cmdDoStuff(_ sender: UIButton) {

DispatchQueue.global(qos: .background).async {
self.doSomething1()
}
}


Related Topics



Leave a reply



Submit