视图如何调用本视图的控制器

2016-07-22  本文已影响75人  梦醒繁华

视图如何调用本视图的控制器

有以下几种方法

-(void)changeInterface:(NSString *)interfacename{  
       Class  class = NSClassFromString(interfacename); 
       UIViewController *con =[[class alloc] init]; 
       if (con == self) { 
       return ; 
        } 
        [self.navigationController pushViewController:con animated:YES];
}

在点击事件发生的同时传递一个指针过去,例如:

pop.delegate = self;

在B视图中实现跳转

[_delegate changeInterface:@"ClassifyViewController"];
#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;
//取出当前选中的导航控制器
UINavigationController *Nav = [tabBarVc selectedViewController];
[Nav pushViewController:topDetailView animated:YES];
上一篇下一篇

猜你喜欢

热点阅读