如何从 UIView 子类推送或呈现 UIViewcontroller 或 storyboard? [英] How to push or present UIViewcontroller or storyboard from UIView subclass?

查看:36
本文介绍了如何从 UIView 子类推送或呈现 UIViewcontroller 或 storyboard?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么:

  1. 我正在使用滚动视图查看一些 UIView(s),并启用分页.
  2. 在 scrollView 子视图的最后一页中,我添加了一个按钮.
  3. 我还写了一个函数,它位于 UIView 的子类中,当我们按下那个按钮时会调用它.
  4. 我想在按下那个按钮时查看故事板.
  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.

推荐答案

需要为自定义UIView类实现协议方法.

You need to implement protocol method for custom UIView class.

例如;

#import <UIKit/UIKit.h>

@protocol YourCustomViewDelegate <NSObject>

- (void)buttonTapped;

@end

@interface YourCustomView : UIView

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

@end

您可以调用 buttonTapped 委托方法的 .m 文件.

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

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

为了正常工作;

  • 在自定义视图控制器中将自定义视图委托设置为 self.
  • 向该视图控制器添加 buttonTapped 方法
  • 然后用那种方法做你想做的事.呈现/推送视图控制器,就像其他答案解释的一样.

编辑

我将尝试说明非常简单的协议用法,希望对您有所帮助.

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

这篇关于如何从 UIView 子类推送或呈现 UIViewcontroller 或 storyboard?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆