我在开发一个项目的时候!在一个视图要有两个子视图来回切换!我用 setHidden 设置bool 值 隐藏和显示。看书的时候,发现iosSDK已经给了相应的函数
//交换俩个视图
[self.view exchangeSubviewAtIndex:<#(NSInteger)#> withSubviewAtIndex:<#(NSInteger)#>];
然后通过
[self.view bringSubviewToFront:viewtwo]; //将子视图提前
[self.viewsendSubviewToBack:viewone]; //将子视图提后
下面写了个demo 验证下
我在父类视图上加了两个子视图分别为viewone和viewtwo 通过分度控件来回切换
- (void)viewDidLoad
{
[superviewDidLoad];
viewone = [[UIViewalloc]initWithFrame:CGRectMake(0,100,self.view.bounds.size.width,self.view.bounds.size.height)];
viewone.backgroundColor = [UIColorgreenColor];
UILabel *label1 = [[UILabelalloc]initWithFrame:CGRectMake(100,100,100, 40)];
label1.text =@"hello";
[label1 setBackgroundColor:[UIColorclearColor]];
[viewone addSubview:label1];
viewone.tag =101;
[self.viewaddSubview:viewone];
viewtwo = [[UIViewalloc]initWithFrame:CGRectMake(0,100, self.view.bounds.size.width,self.view.bounds.size.height)];
viewtwo.backgroundColor = [UIColorgrayColor];
viewtwo.tag =102;
[self.viewaddSubview:viewtwo];
//交换俩个视图
[self.viewexchangeSubviewAtIndex:101withSubviewAtIndex:102];
}
//在故事板中拖拽了一个分段控件
- (IBAction)ChangView:(id)sender
{
UISegmentedControl *segmengt = (UISegmentedControl *)sender;
if (segmengt.selectedSegmentIndex ==0)
{
[self.viewbringSubviewToFront:viewtwo];
//[self.view sendSubviewToBack:viewone];
}
else
{
[self.viewbringSubviewToFront:viewone];
}
}
这样就可以来回切换了!
在项目中有些数据的变更要要通知父类视图!我是通过发送通知,但是也可以通过父类视图查询子视图
// Return an exhaustive descent of the view's subviews
NSArray *allSubviews(UIView *aView)
{
NSArray *results = [aViewsubviews];
for (UIView *eachViewin [aView subviews])
{
NSArray *riz =allSubviews(eachView);
if (riz) results = [resultsarrayByAddingObjectsFromArray:riz];
}
return results;
}
// Return all views throughout the application
NSArray *allApplicationViews()
{
NSArray *results = [[UIApplicationsharedApplication] windows];
for (UIWindow *windowin [[UIApplicationsharedApplication] windows])
{
NSArray *riz =allSubviews(window);
if (riz) results = [resultsarrayByAddingObjectsFromArray: riz];
}
return results;
}
// Return an array of parent views from the window down to the view
NSArray *pathToView(UIView *aView)
{
NSMutableArray *array = [NSMutableArrayarrayWithObject:aView];
UIView *view = aView;
UIWindow *window = aView.window;
while (view != window)
{
view = [view superview];
[array insertObject:viewatIndex:0];
}
return array;
}
- (void) collectViews: (id) sender
{
printf("Subviews of the main view:\n");
//任何视图的子视图
CFShow((__bridgeCFTypeRef)(allSubviews(self.view)));
printf("Path to each main subview:\n");
//每个视图的集合可以观察一个特定的label 上的值或者滑块按钮等
for (UIView *eachViewin allSubviews(self.view))
CFShow((__bridgeCFTypeRef)(pathToView(eachView)));
// More views than you could dream of!
printf("\nAll window subviews:\n");
//每一个窗口
CFShow((__bridgeCFTypeRef)(allApplicationViews()));
}
Demo http://download.youkuaiyun.com/detail/lengshengren/6574425
额外ios sdk 还提供了其他的管理视图的函数
//删除子视图
- (void)removeFromSuperview;
//插入视图
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
//两个视图交换
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
//添加视图
- (void)addSubview:(UIView *)view;
//提前和置后
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
//调用addSubview 成功后给该视图发送didAddSubview 回调 触发uiview 的子类新
增视图时执行其他操作
- (void)didAddSubview:(UIView *)subview;
//回调通知父视图 子视图即将被删除
- (void)willRemoveSubview:(UIView *)subview;
//通知相关视图他们的上级视图有所变化。这些视图也可能需要以上某种方式对新的父类视图做出响应
如何视图从父视图下删除则改视图的新父视图就是为nil
- (void)didMoveToSuperview;
// 移动前发生通知
- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;