获取UIView的UIViewController

万博涛
2023-12-01
@interface UIView
-(UIViewController *) getViewController;
@end
 
@implementation UIView
 
-(UIViewController*) getViewController{
    for(UIView *next =[self superview];next;next=next.superview) {
         UIResponder* nextResponder = [next nextResponder];
         if([nextResponder isKindOfClass:[UIViewController class]]) {
              return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
@end
-(void)buttonPress{
 
     ElectronFrom *electronForm=[[ElectronFrom alloc]init];
 
     [[self getViewController].navigationController pushViewController:electronForm animated:YES];
 
     [electronForm release];
 
}
 类似资料: