ios - Passing Data between View Controllers -
i'm new ios and, objective-c , whole mvc paradigm , i'm stuck following:
i have view acts data entry form , want give user option select multiple products. products listed on view uitableview controller , have enabled multiple selections.
my question is, how transfer data 1 view another? holding selections on uitableview in array, how pass previous data entry form view can saved along other data core data on submission of form?
i have surfed around , seen people declare array in app delegate. read singletons don't understand these , read creating data model.
what correct way of performing , how go it?
this question seems popular here on stackoverflow thought try , give better answer out people starting in world of ios me.
i hope answer clear enough people understand , have not missed anything.
passing data forward
passing data forward view controller view controller. use method if wanted pass object/value 1 view controller view controller may pushing on navigation stack.
for example have viewcontrollera , viewcontrollerb
to pass bool value viewcontrollera viewcontrollerb following.
in
viewcontrollerb.hcreate propertybool@property (nonatomic, assign) bool issomethingenabled;in
viewcontrolleraneed tellviewcontrollerbuse#import "viewcontrollerb.h"then want load view eg.
didselectrowatindexoribactionneed set property inviewcontrollerbbefore push onto nav stack.viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"viewcontrollerb" bundle:nil]; viewcontrollerb.issomethingenabled = yes; [self pushviewcontroller:viewcontrollerb animated:yes];this set
issomethingenabledinviewcontrollerbboolvalueyes.
passing data forward using segues
if using storyboards using segues , need procedure pass data forward. similar above instead of passing data before push view controller, use method called
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender so pass bool viewcontrollera viewcontrollerb following:
in
viewcontrollerb.hcreate propertybool@property (nonatomic, assign) bool issomethingenabled;in
viewcontrolleraneed tellviewcontrollerbuse an#import "viewcontrollerb.h"create segue
viewcontrolleraviewcontrollerbon storyboard , give identifier, in example we'll call"showdetailsegue"next need add method
viewcontrolleracalled when segue performed, because of need detect segue called , something. in our example check"showdetailsegue", if thats performed pass ourboolvalueviewcontrollerb-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender{ if([segue.identifier isequaltostring:@"showdetailsegue"]){ viewcontrollerb *controller = (viewcontrollerb *)segue.destinationviewcontroller; controller.issomethingenabled = yes; } }if have views embedded in navigation controller need change method above following
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender{ if([segue.identifier isequaltostring:@"showdetailsegue"]){ uinavigationcontroller *navcontroller = (uinavigationcontroller *)segue.destinationviewcontroller; viewcontrollerb *controller = (viewcontrollerb *)navcontroller.topviewcontroller; controller.issomethingenabled = yes; } }this set
issomethingenabledinviewcontrollerbboolvalueyes.
passing data back
to pass data viewcontrollerb viewcontrollera need use protocols , delegates or blocks, latter can used loosely coupled mechanism callbacks.
to make viewcontrollera delegate of viewcontrollerb. allows viewcontrollerb send message viewcontrollera enabling send data back.
for viewcontrollera delegate of viewcontrollerb must conform viewcontrollerb's protocol have specify. tells viewcontrollera methods must implement.
in
viewcontrollerb.h, below#import, above@interfacespecify protocol.@class viewcontrollerb; @protocol viewcontrollerbdelegate <nsobject> - (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item; @endnext still in
viewcontrollerb.hneed setupdelegateproperty , synthesize inviewcontrollerb.m@property (nonatomic, weak) id <viewcontrollerbdelegate> delegate;in
viewcontrollerbcall message ondelegatewhen pop view controller.nsstring *itemtopassback = @"pass value viewcontrollera"; [self.delegate additemviewcontroller:self didfinishenteringitem:itemtopassback];that's
viewcontrollerb. inviewcontrollera.h, tellviewcontrolleraimportviewcontrollerb, conform protocol.#import "viewcontrollerb.h" @interface viewcontrollera : uiviewcontroller <viewcontrollerbdelegate>in
viewcontrollera.mimplement following method our protocol- (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item { nslog(@"this returned viewcontrollerb %@",item); }before pushing
viewcontrollerbnavigation stack need tellviewcontrollerbviewcontrolleradelegate, otherwise error.viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"viewcontrollerb" bundle:nil]; viewcontrollerb.delegate = self [[self navigationcontroller] pushviewcontroller:viewcontrollerb animated:yes];
references
- using delegation communicate other view controllers in view controller programming guide
- delegate pattern
Comments
Post a Comment