Ios 初始化后如何在自定义类中更新UILabel文本(Swift 3)

Ios 初始化后如何在自定义类中更新UILabel文本(Swift 3),ios,swift,swift3,storyboard,initialization,Ios,Swift,Swift3,Storyboard,Initialization,我有自定义UIStackView类,但在初始化之后,我无法更改其中的标签,因为它引用了不同的标签对象 // ViewBlocksController.swift // player import UIKit let nowPlayingControl = NowPlayingController() @IBDesignable class TitlesFrame: UIStackView { //Labels variables let songTitle = UILa

我有自定义UIStackView类,但在初始化之后,我无法更改其中的标签,因为它引用了不同的标签对象

//  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()
    }
}
我从MainViewController类调用updateTitles():

//  MainViewController.swift
//  player

import UIKit

class MainViewController: UIViewController {

    let titlesFrame = TitlesFrame()

    //Will call this on custom notification
    func callUpdate(){
        titlesFrame.updateTitles()
    }
}
Print为我提供了以下对象:

<**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>>

当我尝试使用updateTitles()方法更新标签时,它总是更新第一个对象(UILabel:0x100c15b90)。但故事板会显示第二个对象(UILabel:0x100d19d90),并且它永远不会更新


如何更新故事板上的标签?

多亏了@OOPer,我发现问题不在于初始值设定项,而在于如何引用自定义类

为了更新故事板上的标签,我将
让titlesFrame=titlesFrame()
替换为
@IBOutlet var titlesFrame:titlesFrame在我的MainViewController类中。IBOutlet连接到情节提要上的继承UIStackView对象:

//  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()
    }
}

多亏了@OOPer,我发现我的问题不在于初始值设定项,而在于如何引用我的自定义类

为了更新故事板上的标签,我将
让titlesFrame=titlesFrame()
替换为
@IBOutlet var titlesFrame:titlesFrame在我的MainViewController类中。IBOutlet连接到情节提要上的继承UIStackView对象:

//  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()
    }
}

使用
IBOutlets
并在故事板中连接它们。瓦迪安,我在故事板上没有这个标签。只有从自定义类继承的堆栈视图。我以编程方式在类中定义标签,因此我没有任何对象来连接IBOutlet。您不能同时为它们分配一个属性。您需要将其分解为两行来完成此操作,请将行
let titleFrame=titleFrame()
更改为
@IBOutlet var titleFrame:titleFrame,并将其连接到
MainViewController
中的UIStackView(
TitleFrame
)。还有一点,如果不使用像
@OOPer
这样的寻址符号,stackoverflow不会通知我,因此很难知道我有什么评论。使用
IBOutlets
并在storyboard中连接它们。vadian,我在storyboard上没有这个标签。只有从自定义类继承的堆栈视图。我以编程方式在类中定义标签,因此我没有任何对象来连接IBOutlet。您不能同时为它们分配一个属性。您需要将其分解为两行来完成此操作,请将行
let titleFrame=titleFrame()
更改为
@IBOutlet var titleFrame:titleFrame,并将其连接到
MainViewController
中的UIStackView(
TitleFrame
)。还有一点,如果不使用像
@OOPer
这样的寻址符号,stackoverflow不会通知我,所以很难知道我有什么意见。