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?
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.
Comments