xcode - Saving data to an existing plist -
i working on project has simple tableview detail view.
data source plist. trying allow user input saved plist , shown in tableview. have created add view controller gets presented , dismissed modally , has 2 text fields allow user add name of city , name of states, text field input description.
problem: how save data existing plist , show in tableview
. here code table view:
@implementation tableviewcontroller @synthesize content, searchresults; - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; content = [[nsarray alloc] initwithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"data" oftype:@"plist"]]; } - (ibaction)add; { addviewcontroller* controller = [[addviewcontroller alloc] init]; [self presentviewcontroller:controller animated:yes completion:nil]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (tableview == self.searchdisplaycontroller.searchresultstableview) { return [self.searchresults count]; } else { return [self.content count]; } } - (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { nspredicate *resultpredicate = [nspredicate predicatewithformat: @"self['city'] beginswith[c] %@ ", searchtext]; searchresults = [[content filteredarrayusingpredicate:resultpredicate] retain]; } -(bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchstring:(nsstring *)searchstring { [self filtercontentforsearchtext:searchstring scope:[[self.searchdisplaycontroller.searchbar scopebuttontitles] objectatindex:[self.searchdisplaycontroller.searchbar selectedscopebuttonindex]]]; return yes; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier] autorelease]; } if (tableview == self.searchdisplaycontroller.searchresultstableview) { cell.textlabel.text = [[searchresults objectatindex:indexpath.row] valueforkey:@"city"]; cell.detailtextlabel.text = [[searchresults objectatindex:indexpath.row] valueforkey:@"state"]; cell.imageview.image = [uiimage imagenamed:[[self.searchresults objectatindex:indexpath.row] valueforkey:@"cityimage"]]; } else { cell.textlabel.text = [[self.content objectatindex:indexpath.row] valueforkey:@"city"]; cell.detailtextlabel.text = [[self.content objectatindex:indexpath.row] valueforkey:@"state"]; cell.imageview.image = [uiimage imagenamed:[[self.content objectatindex:indexpath.row] valueforkey:@"cityimage"]]; } return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { if (tableview == self.searchdisplaycontroller.searchresultstableview) { [self performseguewithidentifier: @"showdetails" sender: self]; } } -(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"showdetails"]) { nsindexpath *indexpath = [self.tableview indexpathforselectedrow]; detailviewcontroller *dvc = [segue destinationviewcontroller]; if ([self.searchdisplaycontroller isactive]) { dvc.cityimagestring = [[searchresults objectatindex:indexpath.row] valueforkey:@"cityimage"]; dvc.citytextstring = [[searchresults objectatindex:indexpath.row] valueforkey:@"citytext"]; dvc.citynamestring = [[searchresults objectatindex:indexpath.row] valueforkey:@"city"]; dvc.statenamestring = [[searchresults objectatindex:indexpath.row] valueforkey:@"state"]; } else { dvc.cityimagestring = [[self.content objectatindex:indexpath.row] valueforkey:@"cityimage"]; dvc.citytextstring = [[self.content objectatindex:indexpath.row] valueforkey:@"citytext"]; dvc.citynamestring = [[self.content objectatindex:indexpath.row] valueforkey:@"city"]; dvc.statenamestring = [[self.content objectatindex:indexpath.row] valueforkey:@"state"]; } } }
and here code addviewcontroller.h
:
@interface addviewcontroller : uiviewcontroller <uinavigationcontrollerdelegate,uiimagepickercontrollerdelegate>{ iboutlet uitextfield *citytextfield; iboutlet uitextfield *statetextfield; iboutlet uitextview *citydescription; uiimagepickercontroller* imagepicker; } @property (nonatomic, copy) nsstring* name; @property (nonatomic, copy) nsstring* description; @property (nonatomic, strong) uiimage* image; @property (nonatomic, retain) iboutlet uinavigationbar* navigationbar; @property (nonatomic, strong) uitextfield *citytextfield; @property (nonatomic, strong) uitextfield *statetextfield; @property (nonatomic, strong) uitextview *citydescription; @property (nonatomic, strong) iboutlet uibutton* choosephotobutton; @property (nonatomic, strong) iboutlet uibutton* takephotobutton; - (ibaction)save; - (ibaction)cancel; - (ibaction)choosephoto; - (ibaction)takephoto; @end
and @ last add view controller .m
@implementation addviewcontroller @synthesize citytextfield, statetextfield, citydescription; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (ibaction)save { // make sure user has entered @ least recipe name if (self.citytextfield.text.length == 0) { uialertview* alertview = [[uialertview alloc] initwithtitle:@"whoops..." message:@"please enter city name" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; [alertview release]; return; } if (self.statetextfield.text.length == 0) { uialertview* alertview = [[uialertview alloc] initwithtitle:@"whoops..." message:@"please enter city name" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; [alertview release]; return; } // make sure user has entered @ least recipe name if (self.citydescription.text.length == 0) { uialertview* alertview = [[uialertview alloc] initwithtitle:@"whoops..." message:@"please enter city description" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; [alertview release]; return; } self.name = self.citytextfield.text; self.name = self.statetextfield.text; self.description = self.citydescription.text; if ([[self parentviewcontroller] respondstoselector:@selector(dismissviewcontrolleranimated:)]){ [[self parentviewcontroller] dismissviewcontrolleranimated:yes completion:nil]; } else { [[self presentingviewcontroller] dismissviewcontrolleranimated:yes completion:nil]; } } - (ibaction)cancel { { if ([[self parentviewcontroller] respondstoselector:@selector(dismissmodalviewcontrolleranimated:)]){ [[self parentviewcontroller] dismissviewcontrolleranimated:yes completion:nil]; } else { [[self presentingviewcontroller] dismissviewcontrolleranimated:yes completion:nil]; } } } - (ibaction)choosephoto { // show image picker photo library imagepicker = [[uiimagepickercontroller alloc] init]; imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary; imagepicker.delegate = self; imagepicker.allowsediting = yes; [self presentviewcontroller:imagepicker animated:yes completion:nil]; } - (ibaction)takephoto { uiimagepickercontroller *picker = [[uiimagepickercontroller alloc] init]; //picker.delegate = self; picker.allowsediting = yes; picker.sourcetype = uiimagepickercontrollersourcetypecamera; [self presentviewcontroller:picker animated:yes completion:null]; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; if ([citydescription isfirstresponder] && [touch view] != citydescription) { [citydescription resignfirstresponder]; } } - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { [citytextfield resignfirstresponder]; [statetextfield resignfirstresponder]; } #pragma mark - #pragma mark uiimagepickercontrollerdelegate - (void)imagepickercontroller:(uiimagepickercontroller*)picker didfinishpickingmediawithinfo:(nsdictionary*)info { // here when user has picked image. // put image in our property , set on button. if (imagepicker) { self.image = [info objectforkey:uiimagepickercontrollereditedimage]; [self.choosephotobutton setimage:self.image forstate:uicontrolstatenormal]; } else { if (picker) { self.image = [info objectforkey:uiimagepickercontrollereditedimage]; [self.takephotobutton setimage:self.image forstate:uicontrolstatenormal]; } } [self dismissviewcontrolleranimated:yes completion:nil]; [imagepicker release]; imagepicker = nil; } - (void)imagepickercontrollerdidcancel:(uiimagepickercontroller*)picker { [self dismissviewcontrolleranimated:yes completion:nil]; [imagepicker release]; imagepicker = nil; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
i know there tons of similar questions , believe me have looked @ many of them , think hitting wall 1 , cant think of proper way this. may simple life of me cant figure out , thats why trying ask help. appreciate can get. if needs can place sample project on git hub ease of access. project built in ios 6 , latest xcode.
p.s. here link project on git hub: https://github.com/adrianphillips/tablesearch
just every 1 has pointed out can not write plist in main bundle. need copy bundle application document directory. can init , write path in documents directory. other issue have, nothing communicating new data addviewcontroller tableviewcontroller. should create protocol , delegate addviewcontroller.
for gore details see pull request on github.
https://github.com/gayledds/tablesearch.git
Comments
Post a Comment