[Solved] How to push or present UIViewcontroller or storyboard from UIView subclass?


You need to implement protocol method for custom UIView class.

For example;

#import <UIKit/UIKit.h>

@protocol YourCustomViewDelegate <NSObject>

- (void)buttonTapped;

@end

@interface YourCustomView : UIView

@property (nonatomic, strong) id< YourCustomViewDelegate > delegate;

@end

And the .m file you can call buttonTapped delegate method.

- (void)myCustomClassButtonTapped:(id)sender
{
    [self.delegate buttonTapped];
}

For proper work;

  • set your custom view delegate to self in your custom view controller.
  • add buttonTapped method to that view controller
  • And do what you want in that method. Present/Push view controllers like the other answers explained.

Edit

I will try to illustrate very simple usage of protocols, hope it will help you.

#import "MyViewController.h"
#import "YourCustomView.h"

@interface MyViewController ()<YourCustomViewDelegate> // this part is important

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    YourCustomView *customView = // initialize your custom view, I suppose that you already did it
    customView.delegate = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonTapped
{
    YOUR_VIEW_CONTROLLER_CLASS *viewCon =[storyboard instantiateViewControllerWithIdentifier:@"mainVC"];    //mainVC is just a example and u will have to replace it with your viewController storyboard id

    //Pushing VC
    [self.navigationController pushViewController:viewCon animated:YES];
}

6

solved How to push or present UIViewcontroller or storyboard from UIView subclass?