Uiview+viewcontroller.h Click on the view on the controller to make the view push the package of the next view control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.