iOS-在自定義View里面實(shí)現(xiàn)Push方法

在我們平常的開發(fā)過程中、在自定義View里面觸發(fā)點(diǎn)擊方法,實(shí)現(xiàn)Push到另一個(gè)ViewController里面一般通過代理、block、通知等方法

這里介紹一種直接在View里面實(shí)現(xiàn)跳轉(zhuǎn)比較方便的一種方法:

#import <UIKit/UIKit.h>
@interface CustomView : UIView
@end

#import "CustomView.h"
#import "ViewPushViewController.h"
@implementation CustomView

//初始化
- (instancetype)init{
if (self = [super init]) {
    [self createUI];
}
return self;
}

//懶加載
- (UIViewController *)viewController {
for (UIView *next = [self superview]; next; next = next.superview) {
    UIResponder *nextResponder = [next nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return (UIViewController *)nextResponder;
    }
}
return nil;
}

//創(chuàng)建UI
- (void)createUI{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btn];
btn.frame = CGRectMake(100, 100, 100, 50);
btn.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitle:@"點(diǎn)擊跳轉(zhuǎn)" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor yellowColor];
[btn addTarget:self action:@selector(pushToNextController) forControlEvents:UIControlEventTouchUpInside];
}
- (void)pushToNextController{
//需要跳轉(zhuǎn)的ViewController
ViewPushViewController *vc = [[ViewPushViewController alloc]init] ;
vc.hidesBottomBarWhenPushed = YES ;
//通過找到的控制器進(jìn)行跳轉(zhuǎn)
[[self viewController].navigationController pushViewController:vc animated:YES] ;
}
@end

思路:

觸發(fā)點(diǎn)擊事件的控件都是需要加載到控制器所在的view上的,找到控件的事件的響應(yīng)者鏈就可以找到視圖所加載在的Controller,通過這個(gè)controller找到navigationController進(jìn)行跳轉(zhuǎn)。

主要方法:

OC

- (UIViewController *)viewController {
for (UIView *next = [self superview]; next; next = next.superview) {
    UIResponder *nextResponder = [next nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return (UIViewController *)nextResponder;
    }
}
return nil;
}

Swift

public func viewController()->UIViewController? {
        var nextResponder: UIResponder? = self
        repeat {
            nextResponder = nextResponder?.next
            if let viewController = nextResponder as? UIViewController {
                return viewController
            }
        } while nextResponder != nil
        return nil
    }

以上方法最好寫在分類里面、方便調(diào)用
源碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,829評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,180評論 4 61
  • 前幾天,一個(gè)朋友問了我一個(gè)問題:如果不要面子,咱還能活嗎? 我說:能活。 朋友說:那咱還忙乎個(gè)毛啊。擺出一副死豬不...
    歷史人物傳記故事閱讀 263評論 0 0
  • 你本明知,因何退卻。 你固心高,屢歸無成。 你亦不屈,志趣仍在。 你曾憧憬,奈何自怯。 他日稚兒,自是忘憂。 他鄉(xiāng)...
    歸塵于土閱讀 242評論 0 0
  • 今年,我有兩個(gè)親戚小孩高考,而我則理所應(yīng)當(dāng)?shù)漠?dāng)起了免費(fèi)的報(bào)志愿指南,順帶的還有三年來積累的上大學(xué)攻略。 就在昨天還...
    大佳和小米閱讀 846評論 0 3