如何從一個類的ViewController,如何獲取一個UIView上的某種控件


 //獲取view的controller  
 - (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;  
 }  

這是一個封裝好的方法,直接創建一個ViewController的對象就可以調用該方法。

 

 

            for (UIView *subView in view.subviews) {

                if ([subView isKindOfClass:[UIButton class]]) {

                    NSLog(@"subview==%@",subView);

                  

                    }

                }

用該方法遍歷UIView上的所有控件,就可以獲得某種控件,再配合tag可以取到某一個具體的控件。  

 

UINavigationController *parentViewController =  (UINavigationController*)self.navigationController.parentViewController ;

 NSLog(@"parentViewController==%@",parentViewController);

這個方法可以獲取父類的ViewCOntroller


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM