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.h
create propertybool
@property (nonatomic, assign) bool issomethingenabled;
in
viewcontrollera
need tellviewcontrollerb
use#import "viewcontrollerb.h"
then want load view eg.
didselectrowatindex
oribaction
need set property inviewcontrollerb
before push onto nav stack.viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"viewcontrollerb" bundle:nil]; viewcontrollerb.issomethingenabled = yes; [self pushviewcontroller:viewcontrollerb animated:yes];
this set
issomethingenabled
inviewcontrollerb
bool
valueyes
.
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.h
create propertybool
@property (nonatomic, assign) bool issomethingenabled;
in
viewcontrollera
need tellviewcontrollerb
use an#import "viewcontrollerb.h"
create segue
viewcontrollera
viewcontrollerb
on storyboard , give identifier, in example we'll call"showdetailsegue"
next need add method
viewcontrollera
called when segue performed, because of need detect segue called , something. in our example check"showdetailsegue"
, if thats performed pass ourbool
valueviewcontrollerb
-(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
issomethingenabled
inviewcontrollerb
bool
valueyes
.
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@interface
specify protocol.@class viewcontrollerb; @protocol viewcontrollerbdelegate <nsobject> - (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item; @end
next still in
viewcontrollerb.h
need setupdelegate
property , synthesize inviewcontrollerb.m
@property (nonatomic, weak) id <viewcontrollerbdelegate> delegate;
in
viewcontrollerb
call message ondelegate
when pop view controller.nsstring *itemtopassback = @"pass value viewcontrollera"; [self.delegate additemviewcontroller:self didfinishenteringitem:itemtopassback];
that's
viewcontrollerb
. inviewcontrollera.h
, tellviewcontrollera
importviewcontrollerb
, conform protocol.#import "viewcontrollerb.h" @interface viewcontrollera : uiviewcontroller <viewcontrollerbdelegate>
in
viewcontrollera.m
implement following method our protocol- (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item { nslog(@"this returned viewcontrollerb %@",item); }
before pushing
viewcontrollerb
navigation stack need tellviewcontrollerb
viewcontrollera
delegate, 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