ios - Passing data back from a modal view in WatchKit -
when modally presenting, or pushing, interface controller can specify context
parameter pass data new controller 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 reverse?
say present controller modally user pick item list , return main controller, how can item has been picked?
i wrote full example uses delegation in watchkit, passing delegate instance in context, , calling delegate function modal : here full project example on github
here principale classes of example :
interfacecontroller.swift
this main controller, there label , button on view. when press button, presentitemchooser
called , present modalview (modalinterfacecontroller). pass instance of interfacecontroller
in context modal. important controller implements `modalitemchooserdelegate' functions (the protocol definition in 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() { // method called when watch view controller visible user itemselected.settext(item) super.willactivate() } override func diddeactivate() { // method called when watch view controller no longer visible super.diddeactivate() } func didselectitem(itemselected: string) { self.item = itemselected } @ibaction func presentitemchooser() { self.presentcontrollerwithname("modalinterfacecontroller", context: self) } }
modalinterfacecontroller.swift
this class of modal controller. hold reference of previous controller (self.delegate = context as? interfacecontroller
). when row selected, call 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() { // method called when watch view controller visible user super.willactivate() } override func diddeactivate() { // method called when watch view controller 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 how pass data previous controller. if better way let me know, i'll take it. :)
Comments
Post a Comment