Search code examples
iostitlewatchkitwkinterfaceobject

WKInterface button doesn't change title


I'm trying to change the title of a button after I call back from a notification but it doesn't respond at all. I checked it's not nil and checked the text Im' assigning and all is good. I made the property type strong instead of weak but no success.

- (void) setButtonTitleFromSelectedSearchResult:(NSNotification *)notif
{

    [self popController];

    self.sourceMapItem = [[notif userInfo] valueForKey:@"SelectedResult"];

    NSLog(@"The Selected Result is: %@", self.sourceMapItem.name);

    //Testing
    NSLog(@"%@", self.fromButton); // check it's not nil

    [self.fromButton setTitle:self.sourceMapItem.name];
}

Solution

  • With WatchKit, if a user interface element isn't currently visible, it cannot be updated. So, if you've presented another interface controller "on top", you can't update any of the presenting controller's interface elements until you've dismissed the presented controller. At that point, you can safely update the presenting controller in its willActivate method.

    SushiGrass' method of passing blocks is certainly one valid approach. In my testing, however, I ended up having to manage multiple blocks, and many of the subsequent blocks reversed what earlier queued blocks had accomplished (for example, first changing a label's text to "foo", then "bar", then "foo" again. While this can work, it isn't optimal.

    I'd suggest that anyone who is working on a WatchKit app takes a moment to consider how they want to account for off-screen (i.e. not-currently-visible) interface elements. willActivate is your friend, and coming up with a way to manage updates in that method is worthwhile if you're moving from controller to controller.

    For what it's worth, I've encapsulated a lot of this logic in a JBInterfaceController subclass that handles a lot of this for you. By using this as a base class for your own interface controller, you can simply update your elements in the added didUpdateInterface method. Unfortunately, I haven't yet had the time to write proper documentation, but the header files and sample project should get you going: https://github.com/mikeswanson/JBInterfaceController