Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

ios - How to push or present UIViewcontroller or storyboard from UIView subclass?

What I did:

  1. I am using scrollview to view some UIView(s), with paging enabled.
  2. In last page in subview of scrollView I added a button.
  3. I also wrote a function which is in subclass of UIView ,which is invoked when we press that button.
  4. I want to view storyboard when I press that button.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...