ios - When is a an appropriate time to use presentedViewController? -
let's i'm transitioning view controllers segues. have textfield on view controller 1 , label on sec view controller. when i'm segueing 2nd view controller can set label next code:
viewcontroller #2
-(void) viewwillappear: (bool) animated { self.labelvc2.text = ((vc1 *)self.presentingviewcontroller).textfieldvc1.text; }
this makes perfect sense. presentingviewcontroller property beingness used in case. while trying figure out when presentedviewcontroller property used seems logical used first view controller in prepareforsegue method this:
viewcontroller #1
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if([[segue identifier] isequaltostring:@"seguetonextvc"]) { ((vc2 *)self.presentedviewcontroller).labelvc2.text = self.textfieldvc1.text; } }
however doesn't seem work , i'm assuming it's because in prepareforsegue new instance of 2nd view controller gets called moment before segue happens, losing property set. if i'm right in this, when appropriate time utilize presentedviewcontroller property? wouldn't utilize property on 2nd view controller because can access class directly.
take @ class reference uistoryboardsegue
. has couple properties think useful you.
sourceviewcontroller
- "...view controller contents displayed @ origin of segue", i.e. self
destinationviewcontroller
- "...view controller contents should displayed @ end of segue" vc2
trying access through self.presentedviewcontroller
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if([[segue identifier] isequaltostring:@"seguetonextvc"]) { ((vc2 *)destinationviewcontroller).labelvc2.text = self.textfieldvc1.text; } }
back question though... when might want utilize presentedviewcontroller
? reply is, whenever don't have reference presentedviewcontroller
. here's example.
you have bunch of classes have same parent. subviewcontroller1
, subviewcontroller2
, subviewcontroller3
sub-classes of myviewcontroller
, may or may not have 1 presented right now. want check if exists , tell finish it's doing , leave. happily, there method on myviewcontroller
allow that.
if (self.presentedviewcontroller) { myviewcontroller *subvc = (myviewcontroller *)self.presentedviewcontroller; [subvc finishandgoaway]; }
this 1 illustration of might utilize it. there many other circumstances useful, there lots of times when it's totally meaningless because have reference what's presented don't need utilize default one.
ios segue viewcontroller
No comments:
Post a Comment