从 WatchKit 中的模态视图传回数据 [英] Passing data back from a modal view in WatchKit

查看:17
本文介绍了从 WatchKit 中的模态视图传回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

这是示例的主要类:

InterfaceController.swift

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

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

这是我的模态控制器的类.我持有我以前的控制器的引用(self.delegate = context as?InterfaceController).When a row is selected, I call my delegate function didSelectItem(selectedItem) before dismissing it.

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. :)

这篇关于从 WatchKit 中的模态视图传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆