ios - How to push a new controller from UITableViewCell -
i've got 2 classes. 1 itemcontroller
(extends uitableviewcontroller
) , itemcell
(extends 'uitableviewcell').
when each cell clicked push new controller within itemcontroller
's didselectrowatindexpath
.
additionally, in itemcell
, i've got couple of small buttons different tag
each cell. when of these buttons clicked want push new controller. how can this?
self.navigationcontroller.pushviewcontroller
itemcell
doesn't work since doesn't have navigationcontroller
i prefer see solution in rubymotion if not, thats fine :)
edit
i've read delegate
can solution i'm not sure how implement it. i've done
itemcontroller:
def tableview(table_view, cellforrowatindexpath: index_path) data_row = self.data[index_path.row] cell = table_view.dequeuereusablecellwithidentifier(category_cell_id) || begin rmq.create(itemcell.initwithsomething(self), :category_cell, reuse_identifier: category_cell_id).get end cell.update(data_row) cell end
itemcell:
class itemcell < uitableviewcell attr_accessor :delegate def initwithsomething(delegate) @delegate = delegate end ...use @delegate push new controller end
but error
item_controller.rb:114:in
tableview:cellforrowatindexpath:': undefined method
initwithsomething' itemcell:class (nomethoderror)
the general idea cell should tell view controller happened , view controller decided push other view controller.
you can delegate design pattern:
itemcell
has delegate
property conform protocol. example
@class itemcell @protocol itemcelldelegate - (void)itemcelldidclicksubmitbutton:(itemcell *)cell; @end @interface itemcell @property (nonatomic, weak) id<itemcelldelegate> delegate ... @end
in tableview:cellforrowatindexpath:
set controller cell delegate (obviously view controller should conform itemcelldelegate
):
cell.delegate = self
the button on cell trigger ibaction on cell in turn call delegate method
- (ibaction)submitbuttontapped:(id)sender { id <itemcelldelegate> delegate = self.delegate; if ([delegate respondtoselector:@selector(itemcelldidclicksubmitbutton:)]) { [delegate itemcelldidclicksubmitbutton:self]; } }
and in view controller should like:
#pragma mark - itemcelldelegate - (void)itemcelldidclicksubmitbutton:(itemcell)cell { uiviewcontroller *controller = // create view controller push [self.navigationcontroller pushviewcontroller:controller]; }
Comments
Post a Comment