Iphone开发(九)理解subView,手动实现多个视图切换

在Iphone的视图中,其实就是一个一个view,一层view上面放一层view,一个view上面放一群view,甚至UIWindow也是一个view,在网上找了一张图片很能说明这个问题:

《Iphone开发(九)理解subView,手动实现多个视图切换》

可见我们能够看到的都是一个view视图,而我们能对其进行操作,是因为UIController和UIView都是UIResponder的子类。这时我们对视图进行操作时需要掌握几个比较重要的概念和几个常用的方法.一个是superView和subView的概念,一个是UIView和UIControl类对象的区别。

当我们生成一个独立的view视图时,往往是新建一个UIViewController的文件并伴随一个xib文件,这个xib文件中的fie’s owner肯定是这个UIViewController类了,而它的Objects一般就是一个UIView对象(一张干净的画布),我们往往可以将这个UIView改为UIControl,通过修改它的Custom Class项来实现,这时这一大张画布就可以像它上面那些button之流的控件一样来响应方法了,我们在隐藏软键盘时就用到了这一点。

而superView和subView的概念更好理解,view上可以放控件,那么这个view就是这些控件的superView.而UIWindow上可以叠一层又一层的view,UIWindow就是这些view的superView.先放上去的index为0,然后依次累加。

处理superView和subView往往用到几个方法:

removeFromSuperview;//调用者为subView

insertSubview:atIndex;//调用者为superView

了解了这些我们来看一个实例,很简单,实现在根视图控制器上添加两个view(都是整页覆盖屏幕的),然后点击屏幕分别切换显示。

新建一个项目,然后添加两个UIViewController类并附带xib文件,分别取名为FirstViewController,SecondViewController;在viewController中进行如下操作:

viewController.h:

[plain] 
view plain
copy

  1. #import <UIKit/UIKit.h>  
  2. #import “FirstViewController.h”  
  3. #import “SecondViewController.h”  
  4. @interface ViewController : UIViewController  
  5. @property (retain ,nonatomic) FirstViewController *firstViewController;  
  6. @property (retain ,nonatomic) SecondViewController *secondViewController;  
  7. @end  


viewController.m:

[plain] 
view plain
copy

  1. #import “ViewController.h”  
  2.   
  3.   
  4. @implementation ViewController  
  5. @synthesize firstViewController;  
  6. @synthesize secondViewController;  
  7.   
  8.   
  9. – (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.     firstViewController = [[FirstViewController alloc]initWithNibName:@”FirstViewController” bundle:nil];  
  13.     secondViewController = [[SecondViewController alloc]initWithNibName:@”SecondViewController” bundle:nil];  
  14.       
  15.     [self.view addSubview:firstViewController.view];  
  16.     [self.view addSubview:secondViewController.view];  
  17.       
  18. }  
  19.   
  20. – (void)viewDidUnload  
  21. {  
  22.     [super viewDidUnload];  
  23.     // Release any retained subviews of the main view.  
  24. }  
  25.   
  26. – (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  27. {  
  28.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  29. }  
  30.   
  31. @end  

以上代码实现了在viewDidLoad中声明两个对象,然后添加到主视图上,这样在打开模拟器时会看到secondView的视图(后添加的在上面,index为1);

然后在FirstViewController.xib文件中将View的Custom Class发为UIControl,背景改一下,贴个label内容为1;

《Iphone开发(九)理解subView,手动实现多个视图切换》

在FirstViewController.h中添加一个输出口:

[plain] 
view plain
copy

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface FirstViewController : UIViewController  
  4. -(IBAction)clickH:(id)sender;  
  5. @end  


在FirstViewController.m中进行如下操作:

[plain] 
view plain
copy

  1. #import “FirstViewController.h”  
  2.   
  3. @interface FirstViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation FirstViewController  
  8. -(IBAction)clickH:(id)sender  
  9. {  
  10.       
  11.     [self.view.superview insertSubview:self.view atIndex:0];  
  12.     //将当前的view放到最底部。  
  13. }  
  14. – (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  15. {  
  16.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  17.     if (self) {  
  18.         // Custom initialization  
  19.     }  
  20.     return self;  
  21. }  
  22.   
  23. – (void)viewDidLoad  
  24. {  
  25.     [super viewDidLoad];  
  26.     // Do any additional setup after loading the view from its nib.  
  27. }  
  28.   
  29. – (void)viewDidUnload  
  30. {  
  31.     [super viewDidUnload];  
  32.     // Release any retained subviews of the main view.  
  33.     // e.g. self.myOutlet = nil;  
  34. }  
  35.   
  36. – (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  37. {  
  38.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  39. }  
  40.   
  41. @end  

这样就利用insert方法在点击该视图时将其放入底部,另一个视图就显示出来了,在secondViewController中进行同样的操作,就会实现初始化后点击屏幕来回切换的效果。当然,这是在两个视图的情况下,只是两个不停的换位置的效果,demo用于理解方法,如果是多视图大应用的话常常要进行remove操作,这样会提高效率。

《Iphone开发(九)理解subView,手动实现多个视图切换》《Iphone开发(九)理解subView,手动实现多个视图切换》

转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7422365

    原文作者:九宫图算法
    原文地址: https://blog.csdn.net/learnios/article/details/8330850
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞