uiview类里面进行触发按钮事件来让viewcontroller进行跳转的方法

来源:互联网 发布:淘宝魅族mx5屏幕总成 编辑:程序博客网 时间:2024/05/29 08:26

1:

代理,block 都可

说实话代理不熟,一般自己定义一个UIView 中含有button ,并且button的 是push事件我都用block,回调到调用该view的ViewContronller中用
[self.navigationController pushViewController:secondVC animated:YES]  实现push事件。

或者还可以这样
假定你的项目中有文件 UIViewContronller1(继承UIViewContronller), UIViewContronller2(继承于UIViewContronller),UIViewWithButon(继承于UIView)
UIViewWithButon中有一button
在UIViewContronller1中调用UIViewWithButon
目的是:点UIViewWithButon上的Button就会从UIViewContronller1 push到 UIViewContronller2
实现是:
在UIViewWithButon.h文件 @property (nonatomic,strong) UIViewController *owner;
在UIViewWithButon.m文件中button 的方法中 [self.owner.navigationController  pushViewController:[UIViewContronller2 new] animated:YES];

在UIViewContronller1,.m中调用UIViewWithButon
UIViewWithButon *buttonView = [[UIViewWithButon alloc]initwith...];
buttonView.owner = self;
2:

用代理 首先在view 中 声明协议
.h文件
@protocol AccountBindingDelegate <NSObject>
- (void)jumpWebVC:(UIButton*)sender;
@end
@interface CustomRegisterView : UIView
@property (nonatomic, weak) id<AccountBindingDelegate> delegate;
@end

.m文件
[agreementBtn addTarget:self action:@selector(agreementBtnClick:) forControlEvents:UIControlEventTouchUpInside];//button的点击方法

- (void)agreementBtnClick:(UIButton*)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(jumpWebVC:)]) {
        [self.delegate jumpWebVC:sender];
    }
}

在controller中
@interface uiviewContrller ()<AccountBindingDelegate> //遵守协议

@end

- (void)viewDidLoad {
    _registerView.delegate = self;//设置代理
}

//实现方法
- (void)jumpWebVC:(UIButton*)sender
{
    PSMFWebVC *webVC = [PSMFWebVC new];
    UIBarButtonItem *backItem = [UIBarButtonItem new];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
    [self.navigationController pushViewController:webVC animated:YES];
}

0 0
原创粉丝点击