ios - Access text from UITextField in modal View Controller from Home View Controller -
i have viewcontroller1
uilabel
can present viewcontroller2
modal segue. have uitextfield
in viewcontroller2
need access viewcontroller1
can set uilabel
collected text.
i've tried working prepareforsegue
without success. should do?
edit:
i'm using delegate, i'm doing wrong. here's code i'm using in viewcontroller2.h
:
@class viewcontroller2; @protocol vcprotocol -(void)setname:(nsstring *)name; @end @interface viewcontroller2 : uiviewcontroller @property (nonatomic, weak) id<vcprotocol> delegate; @property (strong, nonatomic) iboutlet uitextfield *namefield; - (ibaction)setbutton:(id)sender @end
viewcontroller2.m
-(ibaction)setbutton:(id)sender { [self.delegate setname:namefield.text]; }
i conform vcprotocol
in viewcontroller1.h
. then, in viewcontroller1.m
, have code:
- (void)setname:(nsstring *)name { self.firstsignaturenamelabel.text = name; } - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequal:@"sign"]) { viewcontroller2 *vc = [segue destinationviewcontroller]; vc.delegate = self; } }
you can create protocol , set vc1 delegate of vc2, , using prepareforsegue
set vc1 vc2's delegate should work. know said didn't work, can't see why. have try :
give identifier segue (on storyboard), , implement prepareforsegue
shown below :
vc2delegate.h :
@protocol vc2delegate - (void)updatelabel:(nsstring *)text; @end
vc1.h :
#import "vc2delegate.h" @interface vc1 : uiviewcontroller <vc2delegate> // stuff @end
vc1.m :
- (void)updatelabel:(nsstring *)text { [_label settext:text]; } - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([[segue identifier] isequaltostring:@"yoursegueidentifier"]) { vc2 * vc = [segue destinationviewcontroller]; [vc2 setdelegate:self]; } }
vc2.h :
#import "vc2delegate.h" @interface vc2 : uiviewcontroller @property (weak, nonatomic) id<vc2delegate>delegate; @end
vc2.m
- (void)textwasupdated { // or whatever method detect text has been changed if (_delegate) [_delegate updatelabel:[_textview text]]; }
tell me if works. else, prepareforsegue been called ?
edit : updated answer (wasn't needed). doesn't work :
- is prepareforsegue called ?
- if so, delegate method called ?
- if delegate method isn't called, check delegate not
nil
.
you might want remove segue, , present modally yourself, using presentviewcontroller:animated:completion:
, :
- (ibaction)buttonwastapped { static nsstring * const idmodalview = @"modalview"; static nsstring * const storyboardname = @"mainstoryboard" uistoryboard * storyboard = [uistoryboard storyboardwithname:storyboardname bundle:nil]; vc2 * vc = [storyboard instantiateviewcontrollerwithidentifier:idmodalview]; [vc setdelegate:self]; [self.navigationcontroller presentviewcontroller:vc2 animated:yes completion:nil]; }
Comments
Post a Comment