How to update UILabel text in custom class after initialization (Swift 3)

dandepeched

I have custom UIStackView class but after initialization I cannot change labels in it, because its referring to different label objects.

//  ViewBlocksController.swift
//  player

import UIKit

let nowPlayingControl = NowPlayingController()

@IBDesignable class TitlesFrame: UIStackView {
    //Labels variables
    let songTitle = UILabel()
    let artistAlbumTitle = UILabel()

    //Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        arrangeView()
    }

    required init(coder: NSCoder) {
        super.init(coder: coder)
        arrangeView()
    }

    func arrangeView(){
        //Get initial text
        (songTitle.text, artistAlbumTitle.text) = nowPlayingControl.getTitles()

        //Add labels to the stack
        addArrangedSubview(artistAlbumTitle)
        addArrangedSubview(songTitle)

        //Test what label is used
        print(songTitle)
    }

    func updateTitles(){
        (songTitle.text, artistAlbumTitle.text) = nowPlayingControl.getTitles()
    }
}

I call updateTitles() from my MainViewController class:

//  MainViewController.swift
//  player

import UIKit

class MainViewController: UIViewController {

    let titlesFrame = TitlesFrame()

    //Will call this on custom notification
    func callUpdate(){
        titlesFrame.updateTitles()
    }
}

Print gives me following objects:

<**UILabel: 0x100c15b90**; frame = (0 0; 0 0); text = 'Unknown Artist
\342\200— Unknown ...'; userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x1700956d0>>

<**UILabel: 0x100d19d90**; frame = (0 0; 0 0); text = 'Unknown Artist —
Unknown ...'; userInteractionEnabled = NO; layer = <_UILabelLayer:
0x174097f70>>

When I try to update labels with updateTitles() method, it always update 1st object (UILabel: 0x100c15b90). But Storyboard instead displays 2nd object (UILabel: 0x100d19d90) and it is never updated.

How can I update labels on Storyboard?

dandepeched

Thanks to @OOPer I figured out that my problem was not in the initializers, but in how I refer to my custom class.

To update labels on Storyboard I replaced let titlesFrame = TitlesFrame() with @IBOutlet var titlesFrame: TitlesFrame! in my MainViewController class. IBOutlet is connected to the inherited UIStackView object on the Storyboard:

//  MainViewController.swift
//  player

import UIKit

class MainViewController: UIViewController {

    //IBOutlet is connected to the UIStackView object on the Storyboard
    @IBOutlet weak var titlesFrame: TitlesFrame!

    //Will call this on custom notification
    func callUpdate(){
        titlesFrame.updateTitles()
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to write a Custom UILabel Class

From Dev

How to change Custom Cell's UILabel Text?

From Dev

How to find and set the text to UILabel of UITableViewCell in Swift?

From Dev

How to change text UILabel in swift code?

From Dev

Update Custom UITableViewCell UILabel after asynchronous Parse query

From Dev

Update a UILabel using string value from another class in Swift

From Dev

How to Animate uilabel text between 3 values

From Dev

Swift3: Live UiLabel update on user input

From Dev

Changing text of Swift UILabel

From Dev

Text not fitting in UILabel Swift

From Dev

Swift Class Initialization Confusion

From Dev

UILabel is nil- canot update text of UILabel

From Dev

How to programmatically create UIlabel inside a custom uiView swift

From Dev

How to animate text color change partially in UILabel with custom control?

From Dev

How do you add a UILabel to a UICollectionViewCell without custom class?

From Dev

Update text in button in one opened view after choose something from aonther view in swift 3

From Java

How can you rotate text for UIButton and UILabel in Swift?

From Dev

How to detect if there is a Link in UILabel text and make it clickable - Swift

From Dev

How would one animate UILabel text property with CoreAnimation in Swift?

From Dev

UiLabel text assignment in custom view

From Dev

How would I change the value of a UILabel from a different class in swift?

From Dev

How to get the correct width of a UILabel after the text has been set?

From Dev

Corner radius in custom UILabel class

From Dev

how to update the content in the UIlabel

From Dev

Swift 3 TableView cellForRowAt with array of custom class

From Java

Swift. UILabel text alignment

From Dev

Animate UILabel text color in Swift

From Dev

Detect UILabel text change in swift

From Dev

Printing Parse Class into UILabel (SWIFT)

Related Related

  1. 1

    How to write a Custom UILabel Class

  2. 2

    How to change Custom Cell's UILabel Text?

  3. 3

    How to find and set the text to UILabel of UITableViewCell in Swift?

  4. 4

    How to change text UILabel in swift code?

  5. 5

    Update Custom UITableViewCell UILabel after asynchronous Parse query

  6. 6

    Update a UILabel using string value from another class in Swift

  7. 7

    How to Animate uilabel text between 3 values

  8. 8

    Swift3: Live UiLabel update on user input

  9. 9

    Changing text of Swift UILabel

  10. 10

    Text not fitting in UILabel Swift

  11. 11

    Swift Class Initialization Confusion

  12. 12

    UILabel is nil- canot update text of UILabel

  13. 13

    How to programmatically create UIlabel inside a custom uiView swift

  14. 14

    How to animate text color change partially in UILabel with custom control?

  15. 15

    How do you add a UILabel to a UICollectionViewCell without custom class?

  16. 16

    Update text in button in one opened view after choose something from aonther view in swift 3

  17. 17

    How can you rotate text for UIButton and UILabel in Swift?

  18. 18

    How to detect if there is a Link in UILabel text and make it clickable - Swift

  19. 19

    How would one animate UILabel text property with CoreAnimation in Swift?

  20. 20

    UiLabel text assignment in custom view

  21. 21

    How would I change the value of a UILabel from a different class in swift?

  22. 22

    How to get the correct width of a UILabel after the text has been set?

  23. 23

    Corner radius in custom UILabel class

  24. 24

    how to update the content in the UIlabel

  25. 25

    Swift 3 TableView cellForRowAt with array of custom class

  26. 26

    Swift. UILabel text alignment

  27. 27

    Animate UILabel text color in Swift

  28. 28

    Detect UILabel text change in swift

  29. 29

    Printing Parse Class into UILabel (SWIFT)

HotTag

Archive