ios - Set delegate to self in a subview. Crash -


yes. have uiviewcontroller called navigatorviewcontroller. custom navigation structure includes different "slots" can add content , swipe between these - not important actual problem, code. "slot" number 4 added in navigatorviewcontroller this:

slot4 = [self.storyboard instantiateviewcontrollerwithidentifier:@"view4"]; slot4.view.frame = cgrectmake(screenwidth*2, 0.0, screenwidth, screenheight); [self.view addsubview:slot4.view]; 

which works fine. see uiviewcontroller (view4) added in storyboard on right spot.

inside slot4 uiviewcontroller want add subview. uiviewcontroller called chatviewcontroller. add these lines:

uistoryboard *storyboard = [uistoryboard storyboardwithname:@"main" bundle:nil]; chatviewcontroller *viewcontroller = (chatviewcontroller *)[storyboard instantiateviewcontrollerwithidentifier:@"chat"]; viewcontroller.view.tag = 266; [slot4.view addsubview:viewcontroller.view]; 

so far - works fine. but..

my problem inside chatviewcontroller there uitextview called chattextview. have set this:

@interface chatviewcontroller : uiviewcontroller <uitextviewdelegate> (...)  

in chatviewcontroller header, because want action chattextview. fx. when chattextview "becomes firstresponder", call kind of action. make happen have set

chattextview.delegate = self; 

inside chatviewcontroller's viewdidload method.

but when run project , click on chattextview, crashes. error message saying:

thread 1: exc_bad_access (code=1, address=0xxxxxxxxxx) 

when set chattextview's delegate nil there's no errors, cannot use :-)

please ask if forgot something!

instead of adding chatviewcontroller view subview try this,

uistoryboard *storyboard = [uistoryboard storyboardwithname:@"main" bundle:nil]; chatviewcontroller *viewcontroller = (chatviewcontroller *)[storyboard instantiateviewcontrollerwithidentifier:@"chat"]; viewcontroller.view.tag = 266; [slot4 addchildviewcontroller:viewcontroller];                  [slot4.view addsubview:viewcontroller.view]; [viewcontroller didmovetoparentviewcontroller:slot4];  

use same approach adding slot4 like,

slot4 = [self.storyboard instantiateviewcontrollerwithidentifier:@"view4"]; slot4.view.frame = cgrectmake(screenwidth*2, 0.0, screenwidth, screenheight); [self addchildviewcontroller:slot4];                  [self.view addsubview:slot4.view]; [slot4 didmovetoparentviewcontroller:self];  

Comments