ios - Passing data forward with delegate protocol -
i'm trying pass data forward viewa
viewb
using delegate. reason trying use delegate viewb
slide out menu - using swrevealviewcontroller
library function. side effect - prepareforsegue
not called viewa
not own viewb
.
so seemed delegates way forward.
the problem delegate method not called.
in viewb
- added method gets called when pan gesture happens - opens side menu.
so did:
in viewa
declared protocol:
@protocol firstviewcontrollerdelegate <nsobject> (void)senddata:(nsstring *)string; @end
declared in viewa
@property (nonatomic, assign) id <firstviewcontrollerdelegate> delegate;
then in pan gesture in viewa
-void)pangesturestarted:(uipangesturerecognizer *)gesture{ if (gesture.state == uigesturerecognizerstatebegan){ nsstring *datatopass = @"test sending data second view"; [self.delegate senddata:datatopass]; }
then in viewb
:
#import "viewacontroller.h"
conformed protocol
@interface bbfilterviewcontroller : uiviewcontroller < firstviewcontrollerdelegate>
then in viewb.m
- (void)viewdidload{ [super viewdidload]; viewaviewcontroller *viewa = [[viewaviewcontroller alloc] init]; viewa.delegate = self;} -(void)senddata:(nsstring *)string{ nslog(@"data viewa %@",string);}
the problem in viewb
.m method: -(void)senddata:(nsstring *)string
never called. doing wrong here?
this code:
viewaviewcontroller *viewa = [[viewaviewcontroller alloc] init]; viewa.delegate = self;
creates new instance of viewaviewcontroller
, sets delegate
, destroys it. instead of creating new instance never gets used (its view never displayed) should finding existing instance , connecting that.
in case, using sliding view controller front view controller.
in case delegation isn't best approach relationship sounds backwards. you're looking viewa
pass data viewb
, can directly when provided information sliding controller (which has). viewa
not in same place necessarily, whereas viewb
is.
so, viewb
, send message directly it:
viewbviewcontroller *viewb = (viewbviewcontroller *)[(uinavigationcontroller *)self.revealviewcontroler.rightviewcontroller topviewcontroller]; [viewb ...];
(casts required because methods return superclass pointers , need call subclass specific methods, applied both nav controller , view b controller)
Comments
Post a Comment