視圖如何調(diào)用本視圖的控制器
有以下幾種方法
- 第一種方法在這個自定義的UIVIew設(shè)置一個delegate,指向 A,然后在按鈕事件使用delegate讓A執(zhí)行方法跳轉(zhuǎn)B
在這個自定義的UIVIew—>B
B所載的界面—>A
在A里面定義一個算法如下:
-(void)changeInterface:(NSString *)interfacename{
Class class = NSClassFromString(interfacename);
UIViewController *con =[[class alloc] init];
if (con == self) {
return ;
}
[self.navigationController pushViewController:con animated:YES];
}
在點(diǎn)擊事件發(fā)生的同時傳遞一個指針過去,例如:
pop.delegate = self;
在B視圖中實(shí)現(xiàn)跳轉(zhuǎn)
[_delegate changeInterface:@"ClassifyViewController"];
第二種方法
也可以發(fā)通知給UIViewController A去跳轉(zhuǎn)到B第三種方法
建立一個UIView的擴(kuò)展
UIView+Extent.h文件
#import <UIKit/UIKit.h>
@interface UIView (Extent)
-(UIViewController *)viewController;
@end
UIView+Extent.h文件
#import "UIView+Extent.h"
@implementation UIView (Extent)
-(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;
}
@end
- 第四種方法
//取出根視圖控制器
UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//取出當(dāng)前選中的導(dǎo)航控制器
UINavigationController *Nav = [tabBarVc selectedViewController];
[Nav pushViewController:topDetailView animated:YES];