0

I have an app with 3 pages, when I receive information from my WatchConnectivity delegate, which is my Extension Delegate, I delegate it to the InterfacController to update the UI.

However, the code that handles the update runs, including the UI update, but the UI is actually not updated.

Seems like you can only update UI of interfaceController when it is the current page.

My problem is, if the user goes to a different page when the data comes in, how can I tell when I return to that page, that the UI is updated or not. If I use a Bool to track the state, the Bool will be changed but not the UI.

The data takes time to load so I don't want to reload the data every time willActivate is called.

Jacky Wang
  • 618
  • 7
  • 27

2 Answers2

0

Not likes iOS UIKit, All the WKInterfaceObjects are not actual UI objects. They are just something likes remote controller for remote UI. (You know the bundle contains story board and the bundle contains extensions are separated, in sand-box concept, You can not access UI Objects directly.)

Connection between WKInterfaceObjects and actual UI Objects are established only the connected UI is active. Otherwise, any queries that are sent through WKInterfaceObjects are ignored.

Do likes below:

-(void) willActivate {
    _active = YES;
    [super willActivate];
    if(_needsUpdate){
        [self refresh];
    }
}

-(void) willDeactivate {
    _active = NO;
    [super willDeactivate];
}

// make it to be called when the data changes
-(void) dataDidChange:(NSNotification)* note {
    // If active, refresh UI, otherwise flag it. 
    // It will be handled in next activation.
    if(_active)
        [self refresh];
    else
        _needsUpdate = YES;
}

-(void) refresh {
    __needsUpdate = NO;

    // Update your UI here.
}
jeeeyul
  • 3,727
  • 1
  • 24
  • 37
0
class InterfaceController: WKInterfaceController {
    private var needsUpdate = false
    private var data: AnyObject?


    override func willActivate() {
        super.willActivate()
        updateDataIfExists()
        updateViewIfNeeded()
    }

    private func updateDataIfExists() {
        let update: AnyObject? = "" // get new data from ExtensionDelegate

        if let update = update { // do not forget check if update != data
            data = update
            needsUpdate = true
        }
    }

    private func updateViewIfNeeded() {
        guard needsUpdate else { return }

        // UPDATE UI

        needsUpdate = false
    }
}
Arsen
  • 10,815
  • 2
  • 34
  • 46