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.

  1. in viewcontrollerb.h create property bool

    @property (nonatomic, assign) bool issomethingenabled; 
  2. in viewcontrollera need tell viewcontrollerb use

    #import "viewcontrollerb.h" 

    then want load view eg. didselectrowatindex or ibaction need set property in viewcontrollerb before push 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 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:

  1. in viewcontrollerb.h create property bool

    @property (nonatomic, assign) bool issomethingenabled; 
  2. in viewcontrollera need tell viewcontrollerb use an

    #import "viewcontrollerb.h" 
  3. create segue viewcontrollera viewcontrollerb on storyboard , give identifier, in example we'll call "showdetailsegue"

  4. 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 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 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 in viewcontrollerb bool value yes.

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.

  1. in viewcontrollerb.h, below #import, above @interface specify protocol.

    @class viewcontrollerb;  @protocol viewcontrollerbdelegate <nsobject> - (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item; @end 
  2. next still in viewcontrollerb.h need setup delegate property , synthesize in viewcontrollerb.m

    @property (nonatomic, weak) id <viewcontrollerbdelegate> delegate; 
  3. in viewcontrollerb call message on delegate when pop view controller.

    nsstring *itemtopassback = @"pass value viewcontrollera"; [self.delegate additemviewcontroller:self didfinishenteringitem:itemtopassback]; 
  4. that's viewcontrollerb. in viewcontrollera.h, tell viewcontrollera import viewcontrollerb , conform protocol.

    #import "viewcontrollerb.h"  @interface viewcontrollera : uiviewcontroller <viewcontrollerbdelegate> 
  5. in viewcontrollera.m implement following method our protocol

    - (void)additemviewcontroller:(viewcontrollerb *)controller didfinishenteringitem:(nsstring *)item {     nslog(@"this returned viewcontrollerb %@",item); } 
  6. before pushing viewcontrollerb navigation stack need tell viewcontrollerb viewcontrollera delegate, otherwise error.

    viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"viewcontrollerb" bundle:nil]; viewcontrollerb.delegate = self [[self navigationcontroller] pushviewcontroller:viewcontrollerb animated:yes]; 

references

further help


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -