ios - Passing Data between View Controllers -
i'm new ios and, objective-c , whole mvc paradigm , i'm stuck following:
i have view acts info entry form , want give user alternative select multiple products. products listed on view uitableview
controller , have enabled multiple selections.
my question is, how transfer info 1 view another? holding selections on uitableview
in array, how pass previous info entry form view can saved along other info core info on submission of form?
i have surfed around , seen people declare array in app delegate. read singletons don't understand these , read creating info model.
what right way of performing , how go it?
this question seems popular here on stackoverflow thought seek , give improve reply help out people starting in world of ios me.
i hope reply clear plenty people understand , have not missed anything.
passing info forward
passing info forwards view controller view controller. utilize method if wanted pass object/value 1 view controller view controller may pushing on navigation stack.
for illustration have viewcontrollera
, viewcontrollerb
to pass bool
value viewcontrollera
viewcontrollerb
following.
in viewcontrollerb.h
create property bool
@property(nonatomic) bool *issomethingenabled;
in viewcontrollera
need tell viewcontrollerb
utilize
#import "viewcontrollerb.h"
then want load view eg. didselectrowatindex
or ibaction
need set property in viewcontrollerb
before force onto nav stack.
viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"viewcontrollerb" bundle:nil]; viewcontrollerb.issomethingenabled = yes; [self pushviewcontroller:viewcontrollerb animated:yes];
this set issomethingenabled
in viewcontrollerb
bool
value yes
.
passing info forwards using segues
if using storyboards using segues , need procedure pass info forward. similar above instead of passing info before force view controller, utilize method called
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender
so pass bool
viewcontrollera
viewcontrollerb
following:
in viewcontrollerb.h
create property bool
@property(nonatomic) bool *issomethingenabled;
in viewcontrollera
need tell viewcontrollerb
utilize an
#import "viewcontrollerb.h"
create segue viewcontrollera
viewcontrollerb
on storyboard , give identifier, in illustration we'll phone call "showdetailsegue"
next need add together method viewcontrollera
called when segue performed, because of need observe segue called , something. in our illustration check "showdetailsegue"
, if thats performed pass our bool
value viewcontrollerb
-(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 alter 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
in viewcontrollerb
bool
value yes
.
passing info back
to pass info viewcontrollerb
viewcontrollera
need utilize protocols , delegates or blocks, latter can used loosely coupled mechanism callbacks.
to create viewcontrollera
delegate of viewcontrollerb
. allows viewcontrollerb
send message viewcontrollera
enabling send info 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 setup delegate
property , synthesize in viewcontrollerb.m
@property (nonatomic, weak) id <viewcontrollerbdelegate> delegate;
in viewcontrollerb
phone call message on delegate
when pop view controller.
nsstring *itemtopassback = @"pass value viewcontrollera"; [self.delegate additemviewcontroller:self didfinishenteringitem:itemtopassback];
that's viewcontrollerb
. in viewcontrollera.h
, tell viewcontrollera
import viewcontrollerb
, conform protocol.
#import "viewcontrollerb.h" @interface viewcontrollera : uiviewcontroller <viewcontrollerbdelegate>
in viewcontrollera.m
implement next method our protocol
- (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item { nslog(@"this returned viewcontrollerb %@",item); }
the lastly thing need tell viewcontrollerb
viewcontrollera
delegate before force viewcontrollerb
on nav stack.
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 ios objective-c oop uitableview uiviewcontroller
No comments:
Post a Comment