File name: Uiview+viewcontroller.h
#import <UIKit/UIKit.h>
@interface UIView (Viewcontroller)
-(Uiviewcontroller *) Viewcontoller;
@end
Uiview+viewcontroller.m
#import "Uiview+viewcontroller.h"
@implementation UIView (Viewcontroller)
-(Uiviewcontroller *) Viewcontoller {
Uiresponder *next = Self.nextresponder;
do {
if ([Next Iskindofclass:[uiviewcontroller class]]) {
Return (Uiviewcontroller *) next;
}
Next = Next.nextresponder;
} while (next! = nil);
/**
* End of Loop not found
*/
return nil;
}
@end
Back to the thing a view controller next we look at the specific use
#import "MyView.h"
#import "Uiview+viewcontroller.h"
#import "SecondViewController.h"
Create a new class MyView inherit with UIView, give button an event
@implementation MyView
-(Instancetype) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
Button.frame = CGRectMake (90, 90, 90, 90);
Button.backgroundcolor = [Uicolor Redcolor];
[Button addtarget:self action: @selector (Buttonaction) forcontrolevents:uicontroleventtouchupinside];
[Self Addsubview:button];
}
return self;
}
-(void) Buttonaction {
Secondviewcontroller *secondctrl = [[Secondviewcontroller alloc] init];
[Self.viewContoller.navigationController Pushviewcontroller:secondctrl Animated:yes];
}
In view control, create an object of MyView
MyView *myview = [[MyView alloc] Initwithframe:cgrectmake (0, 0, 375, 200)];
Myview.backgroundcolor = [Uicolor Graycolor];
[Self.view Addsubview:myview];
Also need a push view, this can be set by itself;
Uiview+viewcontroller.h Click on the view on the controller to make the view push the package of the next view control