从WatchKit中的模态视图传回数据

Byt*_*Guy 30 ios apple-watch watchkit

当模态呈现或推送接口控制器时,我们可以指定context参数以将一些数据传递给新控制器,如下所示.

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

// Modal
[self presentControllerWithName:@"MyController" context:[NSDictionary dictionaryWithObjectsAndKeys:someObject, @"someKey", ..., nil]]; 
Run Code Online (Sandbox Code Playgroud)

我的问题是,我们怎么能这样做呢?

假设我们以模态方式呈现一个控制器,以便用户从列表中选择一个项目,然后我们返回主控制器,我们如何获取已被选中的项目?

Pin*_*uch 29

我写了一个完整的例子,它在WatchKit中使用Delegation,在上下文中传递委托实例,并从模态调用委托函数:这是GitHub上的完整项目示例

以下是该示例的原理类:

InterfaceController.swift

这是主控制器,他的视图上有一个标签和一个按钮.按下按钮时,presentItemChooser调用get并显示ModalView(ModalInterfaceController).我将InterfaceController上下文中的实例传递给模态.重要的是这个控制器实现了`ModalItemChooserDelegate'函数(协议定义在模态文件中)

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)

    }
}
Run Code Online (Sandbox Code Playgroud)

ModalInterfaceController.swift

这是我的模态控制器的类.我持有我以前的控制器(self.delegate = context as? InterfaceController)的引用.当选择一行时,我会didSelectItem(selectedItem)在解除它之前调用我的委托函数.

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()
        }


    }
Run Code Online (Sandbox Code Playgroud)

这就是我将数据传递回以前的Controller的方法.如果有更好的方式让我知道,我会接受它.:)

  • 谢谢Pintouch的答案.如果其他人在使用WatchKit时偶然发现问题,那么使用一些代码会很有帮助. (5认同)
  • 由于WatchKit在推送或使用segue时发送上下文对象,与iPhone或iPad上的iOS不同,其中有prepareForSegue,其中实际呈现的VC可用,如何将呈现的视图控制器的委托实际设置为呈现视图控制器?在OP的问题中,没有呈现的视图控制器对象可用于呈现视图控制器,我在文档中找不到合适的方法. (3认同)
  • 代表应该弱吗?也许你的可能会导致保留周期? (2认同)

Stu*_*ner 12

您可以通过在上下文中传递来通过协议传输回信息self:

InterfaceController.m

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

//...

// in some method
[self pushControllerWithName:@"PictureSelectionController" 
                     context:@{@"delegate" : self}];
Run Code Online (Sandbox Code Playgroud)

像这样设置委托:

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"];
    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记声明你的协议:

PictureSelectionController.h

@protocol PictureSelectionControllerDelegate <NSObject>

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

@end
Run Code Online (Sandbox Code Playgroud)

然后你可以从PictureSelectionController.m以下方法调用该方法:

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

并在委托方法中接收它InterfaceController.m:

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