Mike Yan
Mike Yan

Reputation: 1699

Cannot pass 'var' by 'init' into '@IBAction'

I created a pair of xib file with the swift file(for that xib file). [xib_template.xib & view_template.swift] And I want to control this pair of xib file by my [main_VC.swift]. xib file have 1 button and 1 label. I want to change the text of label when I click this button.

I want to set different template view and control them in my [main_VC]. But the @IBAction seems independent inside the class

I pass the value from [main_VC] to [view_template.swift] by init method searched on the internet. I can get correct value by using func in [main_VC]. But when clicking the button, the value is always nil. The var inside IBAction cannot get the value from init.

I am new in swift and I tried my best but still cannot fix this. How can I get the init value inside IBAction? Or how can I programmatically create & disable the Ibaction from [main_VC]?

I adjusted my code to be more easy to read. May have some little typing error.

I searched online and tried all I can already. One people asked similar question before but have no answer. Hope for help. Thanks very much.

[view_template.swift]

import UIKit

class View_template_empty: UIView {

    var _uid: String?
    @IBOutlet weak var labellabel: UILabel!

    init (uid: String) {
            self._uid = uid
            super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    }

    required init?(coder aDecoder: NSCoder) {
    //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }

    @IBAction func clickingPanel2(_ sender: Any) {
        print(self._uid)        // always nil !!!!!!
        self.labellabel.text = “test”
    }

    fun test () {
        print(self._uid)        // correct value
    }
}

[main_VC] (only copy out the main function)

func allocator (_uid: String, uiView: UIView) {
    switch templateType {
        case “one”:
            if let loading_panels = Bundle.main.loadNibNamed("xib_template", owner: uiView, options: nil)?.first as? view_template {
                        loading_panels.translatesAutoresizingMaskIntoConstraints = false
                        uiView.addSubview(loading_panels)

                        loading_panels.leadingAnchor.constraint(equalTo: uiView.leadingAnchor).isActive = true
                        loading_panels.trailingAnchor.constraint(equalTo: uiView.trailingAnchor).isActive = true
                        loading_panels.bottomAnchor.constraint(equalTo: uiView.bottomAnchor).isActive = true
                        loading_panels.topAnchor.constraint(equalTo: uiView.topAnchor).isActive = true

            let view_temp = view_template(uid: _uid)
            view_temp.test()
            }
        case “two”:
            if let loading_panels = Bundle.main.loadNibNamed("xib_template_two”, owner: uiView, options: nil)?.first as? view_template_two {
                        loading_panels.translatesAutoresizingMaskIntoConstraints = false
                        uiView.addSubview(loading_panels)

                        loading_panels.leadingAnchor.constraint(equalTo: uiView.leadingAnchor).isActive = true
                        loading_panels.trailingAnchor.constraint(equalTo: uiView.trailingAnchor).isActive = true
                        loading_panels.bottomAnchor.constraint(equalTo: uiView.bottomAnchor).isActive = true
                        loading_panels.topAnchor.constraint(equalTo: uiView.topAnchor).isActive = true
            }
        default:
                    print("error")
}

Upvotes: 0

Views: 103

Answers (1)

thm
thm

Reputation: 1227

You are using different initializers here:

  • When you say let view_temp = view_template(uid: _uid), init (uid: String) is used and your implementation sets _uid so it is not nil.
  • When you load a view from a XIB, init?(coder aDecoder: NSCoder) is used and this does not set _uid so it is nil.

To inject _uid into your templates, simply say loading_panels._uid = _uid in your two if let loading_panels = ... blocks.

You might also want to read section "Follow case conventions" in the Swift API Design Guidelines to brush up on your naming.

Upvotes: 1

Related Questions