Passing data back from a modal view in WatchKit

13,623

Solution 1

I wrote a full example that uses Delegation in WatchKit, passing the delegate instance in the context, and calling delegate function from the modal : Here is the full project example on GitHub

Here is the principale classes of the example :

InterfaceController.swift

This is the main Controller, there are a label and a button on his view. When you press the button, the presentItemChooser get called and it present the ModalView (ModalInterfaceController). I pass the instance of InterfaceController in the context to the modal. Important this controller implements `ModalItemChooserDelegate' functions (the protocol definition is in the modal file)

class InterfaceController: WKInterfaceController, ModalItemChooserDelegate {

    @IBOutlet weak var itemSelected: WKInterfaceLabel!
    var item = "No Item"

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)

        // Configure interface objects here.

    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        itemSelected.setText(item)
        super.willActivate()

    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    func didSelectItem(itemSelected: String) {
        self.item = itemSelected
    }

    @IBAction func presentItemChooser() {

        self.presentControllerWithName("ModalInterfaceController", context: self)

    }
}

ModalInterfaceController.swift

This is the class of my modal controller. I hold the reference of my previous controller (self.delegate = context as? InterfaceController). When a row is selected, I call my delegate function didSelectItem(selectedItem) before dismissing it.

protocol ModalItemChooserDelegate {
        func didSelectItem(itemSelected:String)
    }

    class ModalInterfaceController: WKInterfaceController {

        let rowId = "CustomTableRowController"

        let items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]

        var delegate: InterfaceController?

        @IBOutlet weak var customTable: WKInterfaceTable!

        override func awakeWithContext(context: AnyObject?) {
            super.awakeWithContext(context)
            self.delegate = context as? InterfaceController
            // Configure interface objects here.
            println(delegate)
            loadTableData()
        }

        override func willActivate() {
            // This method is called when watch view controller is about to be visible to user

            super.willActivate()
        }

        override func didDeactivate() {
            // This method is called when watch view controller is no longer visible
            super.didDeactivate()
        }

        private func loadTableData(){
            customTable.setNumberOfRows(items.count, withRowType: rowId)
            for(i, itemName) in enumerate(items){
                let row = customTable.rowControllerAtIndex(i) as! TableRowController
                row.fillRow(itemName)

            }

        }

        override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
            let selectedItem = items[rowIndex]
            self.delegate?.didSelectItem(selectedItem)
            self.dismissController()
        }


    }

This is how I pass data back to my previous Controller. If is a better way let me know, I'll take it. :)

Solution 2

You can transfer back information via Protocols by passing self within the context:

InterfaceController.m

// don't forget to conform to the protocol!
@interface InterfaceController() <PictureSelectionControllerDelegate>

//...

// in some method
[self pushControllerWithName:@"PictureSelectionController" 
                     context:@{@"delegate" : self}];

And setting the delegate like so:

PictureSelectionController.m

@property (nonatomic, unsafe_unretained) id<PictureSelectionControllerDelegate> delegate;

// ...

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // Configure interface objects here.
    if ([context isKindOfClass:[NSDictionary class]]) {
        self.delegate = [context objectForKey:@"delegate"];
    }
}

Don't forget to declare your protocol:

PictureSelectionController.h

@protocol PictureSelectionControllerDelegate <NSObject>

- (void)selectedPicture:(UIImage *)picture;

@end

Then you can call that method from PictureSelectionController.m:

- (IBAction)buttonTapped {
    // get image
    UIImage *someCrazyKatPicture = //...
    [self.delegate seletedPicture:someCrazyKatPicture];
}

And receive it in the delegate method within InterfaceController.m:

- (void)selectedPicture:(UIImage *)picture {
    NSLog(@"Got me a cat picture! %@", picture);
}

Solution 3

As ghr says, this requires a bit more explanation. The easy (if hacky) way is to make the presenting controller be part of the context that you are passing into the presented controller. That way, you can call back to the presenting controller when you need to. One way to do this is to use an NSDictionary as your context, and store a special key with a reference to the presenting controller. Hope this helps.

Solution 4

Passing data back from watchOS interfaceController using block and segue

Passing data back and forth between interfaceControllers is not so simple. There is segue process in WatchKit but the first problem is that there is no prepareForSegue and you couldn't reach segue's destinationViewController, so you couldn't inject stuffs easily to the new controller (WatchOS 3 - 4). In the backward direction there is no exit so you couldn't reach the unwind segue.

Another problem is that these solutions try to update the data and the user interface of the of the first interfaceController in the willActivate method which is fired any time the watch screen awake - so quite frequently - and this could cause problems and it's complicate.

The programming practice is mainly using delegate and injecting self using the context of the segue, as the above answers describe.

But using delegate is a little bit complicate so I use blocks which is more contemporary and I think better and more elegant.

Let's see how:

First let's prepare the segue in the Interface Builder of the Apple Watch's storyboard, just connect a button with another interfaceController pushing Ctrl button and name the segue.

InterfaceBuilder for Apple Watch storyboard

then in the .h file of the source interfaceController lets's name it SourceInterfaceController.h declare a property for the block:

@property (nonatomic, strong) BOOL (^initNewSessionBlock)(NSDictionary *realTimeDict, NSError *error);

then use contextForSegueWithIdentifier: to transfer the block or any other data to the destination interfaceController using the segueIdentifier if you have more segues.

This Apple method actually use a (id)context as a return object which could be any object and the destination interfaceController's awakeWithContext:(id)context method will use it when the the interfaceController launches.

So let's declare the block in SourceInterfaceController.m then pass it to the context:

- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier {

    __unsafe_unretained typeof(self) weakSelf = self;

    if ([segueIdentifier isEqualToString:@"MySegue"]) {

        self.initNewSessionBlock =  ^BOOL (NSDictionary *mySegueDict, NSError *error)
        {
            [weakSelf initNewSession];
            NSLog(@"message from destination IC: %@", realTimeDict[@"messageBack"]);
            return YES;
        };

        return self.initNewSessionBlock;
    }
    else if ([segueIdentifier isEqualToString:@"MyOtherSegue"]) {

        self.otherBlock =  ^BOOL (NSString *myText, NSError *error)
        {
            //Do what you like
            return YES;
        };

        return self.otherBlock;

    }
    else {
        return nil;
    }

}

If you'd like to transfer any more data than just the block with the context to the destination interfaceController, just wrap them in a NSDictionary.

In the destination interfaceController name it DestinationInterfaceController.h let's declare another property to store the block using any name but the same variable declaration

@property (copy) BOOL (^initNewSessionBlock)(NSDictionary *realTimeDict, NSError *error);

then fetch the block from the context in DestinationInterfaceController.m:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    self.initNewSessionBlock = context;
}

Later in DestinationInterfaceController.m just trigger the block, for example in an action method with a button:

- (IBAction)initNewSessionAction:(id)sender {

    NSError *error = nil;
    NSDictionary *realTimeDict = @{@"messageBack" : @"Greetings from the destination interfaceController"};

    BOOL success = self.initNewSessionBlock(realTimeDict, error);
    if (success) {
        [self popController];
    }

}

The block will be executed any method of the source interfaceController using the data in the scope of the destination interfaceController, so you can send data back to the destination sourceController. You can pop the interfaceController using popController if everything is ok and the block return yes as a BOOL.

Note: Of course you can use any kind of segue whether it's a push or modal and you can use pushControllerWithName:context: too to trigger the segue, and you can use this method's context in the same way.

Solution 5

I've been testing passing self to the controllers (modal or not) and using didDeactivate as a way to invoke the delegate methods, but the catch is that it's called whenever the screen is dismissed or when a new view is presented. I'm just getting started with WatchKit so I could be totally wrong here.

My delegate

@class Item;
@class ItemController;
@protocol AddItemDelegate <NSObject>
- (void)didAddItem:(ItemController *)controller withItem:(Item *)item;

My root controller

@interface ListController() <AddItemDelegate>
...
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
    // TODO: How do we pass data back? Delegates? Something else?
    if ([self.items[rowIndex] isEqualToString:@"Item 1"]) {
        // TODO: Do I really want to pass along a single object here?
        [self pushControllerWithName:@"Item" context:self];
    }
}
...
#pragma mark - AddItemDelegate
- (void)didAddItem:(ItemController *)controller withItem:(Item *)item {
    NSLog(@"didAddItem:withItem: delegate called.");
}

My child controller

@property (nonatomic, strong) Item *item;
@property (nonatomic, weak) id<AddItemDelegate> delegate;
...
- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // TODO: Check that this conforms to the protocol first.
    self.delegate = context;
}
...
- (void)didDeactivate {
    [super didDeactivate];

    [self.delegate didAddItem:self withItem:self.item];
}
Share:
13,623
BytesGuy
Author by

BytesGuy

Updated on June 07, 2022

Comments

  • BytesGuy
    BytesGuy almost 2 years

    When modally presenting, or pushing, an interface controller we can specify the context parameter to pass some data to the new controller as follows.

    // Push
    [self pushControllerWithName:@"MyController" context:[NSDictionary dictionaryWithObjectsAndKeys:someObject, @"someKey", ..., nil]]; 
    
    // Modal
    [self presentControllerWithName:@"MyController" context:[NSDictionary dictionaryWithObjectsAndKeys:someObject, @"someKey", ..., nil]]; 
    

    My question is, how can we do the reverse?

    Say we present a controller modally for the user to pick an item from a list and we return to the main controller, how can we get the item that has been picked?