Search code examples
objective-cuiviewcontrolleruinavigationcontrolleraddsubview

Navigate to another view controller from a subview


I'm trying to navigate from a subview to another viewcontroller.

I'm adding my subview like below,

EducatorDetailsViewController(Parent view)

adultV=[[AdultOccupantV alloc]init];
adultV.frame = CGRectMake(0, 0, 1024, 768);
adultV.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
adultV.delObj=self;
adultV.hidden=YES;
[self.view addSubview:adultV];

I have tried to navigate to my LoginViewController from the subview using below code.

LoginVC *login =[[LoginVC alloc] init];
[self.navigationController pushViewController:login animated:YES];

But this code gives an error because there is no navigational controller for the subview. I have found a similar question here which was asked some years ago.It actually has an answer but I couldn't understand the answer since I am very new to Objective C.It will be really great if someone can help me to find a way to navigate from subview to another view controller.


Solution

  • There's design pattern called proxy pattern or delegate pattern. You can check it in wiki.

    In ur example, view shouldn't do navigate actions but view's super view controller does. So the ViewController should be the delegate of ur view.