Reasons for an IBOutlet to be nil

前端 未结 6 1924
滥情空心
滥情空心 2021-01-12 13:44

What are the reasons why an IBOutlet (connected) could be nil?

I have one in may application which is always nil, even if I re

6条回答
  •  萌比男神i
    2021-01-12 14:30

    One possibility: Suppose the IBOutlet container is a singleton object with a function like:

    + (singletonObject*) sharedInstance {
        if(!gGlobalSingletonPointer) {
            gGlobalSingletonPointer = [[singletonObject alloc] init];
        }
    
        return gGlobalSingletonPointer;
    }
    

    You create the singleton object "on demand" if it doesn't already exist. You save a global pointer to it, as you create it, in that function.

    If you also instantiate such an object in InterfaceBuilder, and connect its outlets, this object will be created without sharedInstance being called. If you subsequently call sharedInstance, a new object is created (sans IBOutlet connections).

    The solution is to update the global pointer in singletonObject's init or awakeFromNib function.

提交回复
热议问题