简体   繁体   English

在Swift UIView XIB中全局初始化变量为'nil'?

[英]Globally initialized variable changes to 'nil' in Swift UIView XIB?

I have globally initialised variable inside a UIView class. 我在UIView类中有全局初始化变量。

The variable is statusLabel as string. 变量是statusLabel as string。 I have assigned a value for statusLabel in Pickerview didSelectRow delegate method. 我在Pickerview didSelectRow委托方法中为statusLabel分配了一个值。 There i'm getting the value for string. 在那里我得到了字符串的价值。 However When the delegate finishes the value of string changes to nil 但是当委托完成字符串更改的值为nil

It's all same for all other Globally initialised variable. 对于所有其他全局初始化变量,它都是相同的。 What Could be the reason? 可能是什么原因?

Thanks in Advance 提前致谢

class CustomPickerMenu: UIView,UIPickerViewDataSource,UIPickerViewDelegate
{
    var statusLabel = String()

    var pickerArray = [String]()

    required init?(coder aDecoder: NSCoder)
    {
        pickerArray = ["None","Connections","All"]
        super.init(coder: aDecoder)
         print("Awake with coder")

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if(row == 0)
        {
            statusLabel = pickerArray[row]
        }
        if(row == 1)
        {
            statusLabel = pickerArray[row]
        }
        if(row == 2)
        {
            statusLabel = pickerArray[row]
        }
      print("statusLabel : " + statusLabel)
    }

    @IBAction func saveStatus(_ sender: Any)
    {   
        delegate?.statusChange(controller: self, text:statusLabel)
        self.delegate?.showNavigationController(controller: self)

        print("statusLabel : " + statusLabel)
    }

here is the output for statusLabel during didselectrow delegate(1st three outputs) and save status(last output) 这是在didselectrow委托(前三个输出)和保存状态(最后输出)期间statusLabel的输出 在此输入图像描述

I found the solution, more than that it was a mistake. 我找到了解决方案,而不是错误。 the class the I used "CustomPickerMenu" got called more than one time. 我使用过的“CustomPickerMenu”类不止一次被调用过。 During init(frame) and init(decoder). 在init(帧)和init(解码器)期间。 Thats the reason the value of string initially assigns during 1st call and changes to nil during 2nd call. 这就是字符串的值在第一次调用期间最初分配的原因,并且在第二次调用期间变为nil。

I initially connected the delegates to xib through the storyboard, Then I called the xib class from one view controller, which will eventually be calling the xib init(frame). 我最初通过故事板将委托连接到xib,然后我从一个视图控制器调用xib类,最终将调用xib init(frame)。 I wrote the code to load the xib in it's init(frame). 我编写了代码来加载xib的init(frame)。 Since I already connected the xib with the storyboard, xib will also call it init(coder) method. 由于我已经将xib与storyboard连接,因此xib也将其称为init(coder)方法。

so what I did was, removed the delegates from storyboard and called "self.pickerView.delegate" and "self.pickerView.dataSource" in its init(frame), eventually the init(coder) method stop calling. 所以我所做的是,从storyboard中删除了委托,并在其init(框架)中调用了“self.pickerView.delegate”和“self.pickerView.dataSource”,最终init(编码器)方法停止调用。

Now, the value of string is not changing. 现在,string的值不会改变。

Do these checks and see if your code follows the same. 执行这些检查,看看您的代码是否遵循相同的规则。 delegate is a weak variable. delegate是一个微弱的变量。

  • As delegate is an optional, make sure you do not force unwrap it. 由于委托是可选的,请确保您不要强行打开它。 Use ? ? instead of ! 而不是! .

  • Make sure the Protocol is being implemented by the class that this delegate points to. 确保该委托指向的类正在实现协议。

  • Make sure that delegate variable is assigned with self in the implemented class. 确保在实现的类中为self赋值delegate变量。

A sample playground demo would be. 一个示例游乐场演示将是。

class CustomPickerMenu
{
    var statusLabel = String()

    var pickerArray = [String]()
    weak var delegate:Sample?

    func call() {
        pickerArray = ["One", "Two"]
        print(pickerArray)
    }
    func pick()
    {
        statusLabel = pickerArray[0]
        print(statusLabel)
    }

    @IBAction func saveStatus(_ sender: Any)
    {
        print(statusLabel)
        self.delegate?.doSomething(data: statusLabel)
        print(statusLabel)
    }
}

class Apply: Sample {

    func test() {
        let picker = CustomPickerMenu()
        picker.call()
        picker.pick()
        picker.delegate = self
        picker.saveStatus(self)
    }
    func doSomething(data: String) {
        print(data)
    }
}

let apply = Apply()
apply.test()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM