variables returning null value upon switching View Controllers in Objective C -
im new programming objective c , working on moving data between view controllers. wondering if bi-directional flow of data (variables) between viewcontrollers possible. can move data backwards (to presentingviewcontroller / sourceviewcontroller) cannot move data forward (to presentedviewcontroller / destinationviewcontroller).
i have made simple case scenario (involving strings principle of idea) of , involves updating uitextfield on destinationviewcontroller using uilabel in sourceviewcontroller , vice-versa. cannot update uitextfield using uilabel, can update uilabel using uitextfield. have made logs of different statements track variable values when switch viewcontrollers variables data returns null if marked strong.
can please offer guidance, been tearing away @ mind, or missing obvious? don't why keep getting (null) value (in nslog) when switch viewcontrollers.
- my sourceviewcontroller / presentingviewcontroller named "viewcontroller."
- my destinationviewcontroller / presentedviewcontroller named "gears2viewcontroller".
i have attached code files below:
viewcontroller.h:
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller @property (strong, nonatomic) iboutlet uilabel *outputlabel; - (ibaction)exittohere:(uistoryboardsegue *)sender; @end
viewcontroller.m:
#import "viewcontroller.h" #import "gear2viewcontroller.h" @interface viewcontroller () - (ibaction)changeitem:(id)sender; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)changeitem:(id)sender { gear2viewcontroller *g2vc=[[gear2viewcontroller alloc] init]; g2vc.petersido=[nsstring stringwithformat:@"%@",self.outputlabel.text]; [self performseguewithidentifier:@"togear2" sender:self]; nslog(@"viewcontroller uilabel reads %@",g2vc.petersido); } - (void) prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { } - (ibaction)exittohere:(uistoryboardsegue *)sender { } @end
gears2viewcontroller.h:
#import <uikit/uikit.h> #import "viewcontroller.h" @interface gear2viewcontroller : uiviewcontroller @property (strong, nonatomic) nsstring *petersido; @end
gears2viewcontroller.m:
@interface gear2viewcontroller () @property (strong, nonatomic) iboutlet uitextfield *updatedoutput; - (ibaction)updateoutput:(id)sender; @end @implementation gear2viewcontroller - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; nslog(@"gears2viewcontroller viewdidappear reads %@",self.petersido); } - (void)viewdidload { [super viewdidload]; nslog(@"gears2viewcontroller viewdidload responds %@",self.petersido); // additional setup after loading view. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)updateoutput:(id)sender { self.petersido = self.updatedoutput.text; ((viewcontroller *)self.presentingviewcontroller).outputlabel.text = self.petersido; nslog(@"gears2view controller updating viewcontroller uilabel reads %@",self.petersido); } @end
nslog:
2015-06-29 18:52:58.798 testerbeta[21735:645772] gears2viewcontroller viewdidload responds (null) 2015-06-29 18:52:58.799 testerbeta[21735:645772] viewcontroller uilabel reads pie 2015-06-29 18:52:59.317 testerbeta[21735:645772] gears2viewcontroller viewdidappear reads (null) 2015-06-29 18:53:12.651 testerbeta[21735:645772] gears2view controller updating viewcontroller uilabel reads no dont
quite lengthy in advance!!!
you want pass data in prepareforsegue:
, so:
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)__unused sender { if ([[segue identifier] isequaltostring:@"togear2"]) { gear2viewcontroller *controller = (gear2viewcontroller *)segue.destinationviewcontroller; controller.petersido = self.outputlabel.text; } }
the reason why segue instantiates presented view controller you, , set property of instantiated view controller segue present.
to pass data back, can use unwind segue, can value presented view controller's property.
- (ibaction)unwindfromgear2:(uistoryboardsegue *)segue { gear2viewcontroller *controller = (gear2viewcontroller *)segue.sourceviewcontroller; self.outputlabel.text = controller.petersido; }
this proper way pass data , forth via segues. gear2viewcontroller shouldn't setting properties on presentingviewcontroller.
update:
the preferred way test property isn't nil this:
if (self.petersido) { self.updatedoutput.text = self.petersido; } else // no need if test here { self.updatedoutput.text = @""; }
that's long form, assignment , if test can more concisely written as:
self.updatedoutput.text = self.petersido ?: @"";
Comments
Post a Comment