开发者

Problems with NSNotification(s) not working on view controllers that have not been 'accesses' yet

开发者 https://www.devze.com 2023-02-20 12:09 出处:网络
so, I\'m using the NSNotification to pass messages around as I get data from my server. The problem is that the notifications are not being responded to if the controller has not been \"accessed\" yet

so, I'm using the NSNotification to pass messages around as I get data from my server. The problem is that the notifications are not being responded to if the controller has not been "accessed" yet. What I mean is, in my app I have a SignInViewController and a UITabBarController in the MainWindow xib. In the app delegate both of their views are added to the window with the sign in view being on top. This effectively "accesses" the corresponding controllers. In the case of the UITabBarController the view belongs to the DashboardViewController.

When a user signs in successfully from the SignInViewController I get a JSON response from the server which is broken down and then messaged to listeners that parse the individual chunks. Once they are parsed the final objects are then messaged out as well to be picked up by the controllers who have to use them.

All of that assumingly works perfectly fine, except only one chunk is actually being received and that happens to be the one targeted for the DashboardViewController and since the controller's view was already set way beforehand, the controller is "instanced" and can receive the message. Every other controller down the line has not been accessed (instanced?) yet, so they don't receive the messages and the objects being sent go into oblivion.

The only so called solution I've found so far, 开发者_如何学Gowas to add the views of the remaining controllers beneath the dashboard view in the app delegate. If I do that, the message passing works, but that feels like the wrong way of doing it because the views will not be used at all and their just wasting space (memory?) in the subviews array.

So, I was wondering if there's a better way of making sure all the controllers will receive the objects they're being sent.

On a side note, the app used to work just fine, but I went back to synchronise the code between all the object passing, or in other words I converted it to messages and as soon as I did that it stopped working as it was and I am now in my current state...

I'd appreciate any help. <3

UPDATE

- (id)init {
    self = [super init];

    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDevicesFromNotification:) name:DevicesParsed object:nil];
    }

    return self;
}


It sounds like you're adding the listeners in the various view controllers' viewDidLoad, or possibly in the init method of custom views in those controllers. What it sounds like you should be doing is adding the listeners in the controllers' init methods, so they are registered for the messages when you first create them.


It turns out that the method that should have been used to setup the NSNotification(s) is awakeFromNib. The init and initWithNibName:bundle: methods will not be called automatically, only when you're doing the instancing yourself (I could be wrong, but that's the pattern I've seen so far).

0

精彩评论

暂无评论...
验证码 换一张
取 消