Using presentViewController from UIView
Only UIViewController can present another view controller, so if you need to show a viewcontroller from view there are several way, one of them is like this:
make a viewcontroller on which your parent view is situated a delegate of it
ParentView *view = [ParentView new];
...
view.delegate = self;
then, inside ParentView call method of that delegate
- (IBAction)homeButtonPressed:(id)sender {
[self.delegate buttonPressed];
}
and then, inside your VC implement
-(void)buttonPressed {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:vc animated:NO completion:NULL];
}
If you need to keep this code inside UIView and avoid delegation you can do a trick like this (personally i don't like it but it should work)
-(void)buttonPressed{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[(UIViewController*)self.nextResonder presentViewController:vc animated:NO completion:NULL];
}
You can do this without using the delegate pattern. Here you go
ObjectiveC
UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController.........
- (UIViewController *)currentTopViewController {
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}
Swift
var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
topVC = topVC!.presentedViewController
}
topVC?.presentViewController........