ios - Passing String Value of a NSManagedObject attribute to a new view controller -
i have tableview
populated nsstring
values nsfetchedresultscontroller
. have cells configured check/uncheck cells. works perfectly.
i'm trying configure cells clicking 1 of labels within cell triggers segue view controller. configured label , threw log statement in i'd segue , works properly. when add segue, no compiler errors, crashes @ runtime follow error:
2015-05-31 08:09:04.656 myapp[17682:1240919] -[uiviewcontroller selecteditemphoto:]: unrecognized selector sent instance 0x7fa42b65d4e0 2015-05-31 08:09:04.694 myapp[17682:1240919] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uiviewcontroller selecteditemphoto:]: unrecognized selector sent instance 0x7fa42b65d4e0'
i'm i'm doing startlingly stupid, i'm stumped what. welcome suggestions.
mycustomcell.h:
@property (strong, nonatomic) iboutlet uilabel *itemdescription; @property (strong, nonatomic) iboutlet uilabel *itemgroup; @property (strong, nonatomic) iboutlet uiimageview *itemimage;
firstvc.m methods:
cellforrowatindexpath
method
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // implement custom cell mycustomcell *customcell = [tableview dequeuereusablecellwithidentifier:@"customcell" forindexpath:indexpath]; // hold of mymanagedobject frc mynsmanagedobject *myobject = [self.fetchedresultscontroller objectatindexpath:indexpath]; // configures 1st label hyperlink customcell.itemdescription.text = myobject.itemdescription; customcell.itemdescription.textcolor = [uicolor bluecolor]; customcell.itemdescription.userinteractionenabled = yes; // enables gesture , sets demoobject pass along via segue called labeltap uitapgesturerecognizer *labeltapgesture = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(labeltap)]; [customcell.itemdescription addgesturerecognizer:labeltapgesture]; self.demoobject = myobject; // configures 2nd label within customcell customcell.itemgroup.text = myobject.itemgroup; // add image cell, imported images.xcassets nsstring *imagename = [nsstring stringwithformat:@"%@-1.jpg", myobject.photo]; customcell.imageview.image = [uiimage imagenamed:imagename]; // following code ensures random checkmarks don't appear when user scrolls. if ([self.selectedobjects containsobject:[self.fetchedresultscontroller objectatindexpath:indexpath]]) { customcell.accessorytype = uitableviewcellaccessorycheckmark; } else { customcell.accessorytype = uitableviewcellaccessorynone; } return customcell; }
didselectrowatindexpath
method
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { self.selectedobject = [self.fetchedresultscontroller objectatindexpath:indexpath]; // set checkmark accessory selected row. if ([tableview cellforrowatindexpath:indexpath].accessorytype == uitableviewcellaccessorynone) { [[tableview cellforrowatindexpath:indexpath] setaccessorytype:uitableviewcellaccessorycheckmark]; [self.selectedobjects addobject:self.selectedobject]; [tableview deselectrowatindexpath:indexpath animated:yes]; } else { [[tableview cellforrowatindexpath:indexpath] setaccessorytype:uitableviewcellaccessorynone]; [self.selectedobjects removeobject:self.selectedobject]; [tableview deselectrowatindexpath:indexpath animated:yes]; } // enables save button if there items in selectedobjects array if (self.selectedobjects.count > 0) { [self.savebutton setenabled:yes]; } else { [self.savebutton setenabled:no]; } }
labeltap
method
- (void) labeltap { // "hyperlink" effect i'm trying achieve works without segue nslog(@"itemdescription tapped"); [self performseguewithidentifier:@"mysegue" sender:nil]; }
prepareforsegue
method
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([[segue identifier] isequaltostring:@"mysegue"] && self.demoobject != nil) { // line returns value... nslog(@"self.demoobject = %@", self.demoobject.itemdescription); // ...but crashes here when tries set on destinationviewcontroller secondviewcontroller *destinationviewcontroller = [segue destinationviewcontroller]; destinationviewcontroller.selecteditemphoto = self.demoobject.photo; destinationviewcontroller.selecteditemtitle = self.demoobject.itemdescription; } }
secondviewcontroller.h properties
// photo string references filename in app @property (nonatomic, strong) nsstring *selecteditemphoto; @property (nonatomic, strong) nsstring *selecteditemtitle;
from error seems did not set class of second view controller. seems uiviewcontroller
, should secondviewcontroller
.
in storyboard select second view controller , set class secondviewcontroller
.
another advice in objective c should use introspection before casting object. in code add:
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([[segue identifier] isequaltostring:@"mysegue"] && self.demoobject != nil) { // line returns value... nslog(@"self.demoobject = %@", self.demoobject.itemdescription); // ...but crashes here when tries set on destinationviewcontroller if ([[segue destinationviewcontroller] iskindofclass:[secondviewcontroller class]]) { secondviewcontroller *destinationviewcontroller = (secondviewcontroller *)[segue destinationviewcontroller]; destinationviewcontroller.selecteditemphoto = self.demoobject.photo; destinationviewcontroller.selecteditemtitle = self.demoobject.itemdescription; } } }
in way segue won't happen app won't crash.
Comments
Post a Comment