#import <UIKit/UIKit.h> @interface ParentClassView : UIView #pragma mark-獲取當前view上層的Controller -(UIViewController *)getController; @end
#import "ParentClassView.h" @implementation ParentClassView #pragma mark-獲取當前view上層的Controller -(UIViewController*)getController{ id controller = [self nextResponder]; while (![controller isKindOfClass:[UIViewController class]]&&controller!=nil) { controller = [controller nextResponder]; } UIViewController *mainController=(UIViewController*)controller; return mainController; } ParentClassView這個類繼承成的是UIView 假如你在這個View中添加了一個按鈕這個按鈕的功能就是跳轉頁面 方案一: 在Controller中把本身傳到ParentClassView這個View中來解決跳轉問題 方案二:我推薦使用的就是GitController方法 使用只需這樣寫就OK啦 : 如: NewsController * newsController=[NewsController new]; //跳轉風格 newsController.modalTransitionStyle=UIModalTransitionStylePartialCurl; //[self getController]--------得到Controller //模態跳轉 [[self getController] presentViewController:newsController animated:YES completion:nil];