cell中如何简单实现跳转VC

2019-03-06  本文已影响0人  有梦才可以远航

在ios中cell属于常用控件之一,在cell中设置按钮,该如何跳转其他VC那?

\ 如下:

//设置view的类扩展(extension)UIViewController

#import <UIKit/UIKit.h>

@interface UIView (UIViewController)

- (UIViewController *)viewController;

@end
#import "UIView+UIViewController.h"

@implementation UIView (UIViewController)

- (UIViewController *)viewController {
    
    //通过响应者链,取得此视图所在的视图控制器
    UIResponder *next = self.nextResponder;
    do {
        //判断响应者对象是否是视图控制器类型
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        
        next = next.nextResponder;
        
    }while(next != nil);
    
    return nil;
}

@end

实际应用中如下写法即可:

@implementation TestTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 200, 100)];
    btn.backgroundColor = [UIColor blueColor];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.recordButton];
}
//只需self.viewController就可找到navigationController进行push
- (void)btnClicked:(UIButton *)sender{
    Test2ViewController *vc = [[Test2ViewController alloc]init];
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

欢迎互相学习Github

上一篇下一篇

猜你喜欢

热点阅读