2013-04-23 54 views
1

我有一个3页的基于页面的应用程序,每个页面上我都有一个UIView作为子视图,这个UIViewUIViewController加载。在最后一页,我有一个UICollectionView包含不同的项目,当我碰到UICollectionCell我想改变子视图。以编程方式更改子视图控制器

这是我的故事板:

enter image description here

橙色UIView装有意见从UIViewController S(用XIB1文件)

而且viewWillAppear:DataViewController.m

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    self.dataLabel.text = [self.dataObject description]; 

    if([self.dataLabel.text isEqualToString:@"View1"]) 
     [mainView addSubview: view1.view]; 
    else if([self.dataLabel.text isEqualToString:@"View2"]) 
     [mainView addSubview: view2.view]; 
    else if([self.dataLabel.text isEqualToString:@"View3"]){ 
     [mainView addSubview: view3.view]; 
    } 
} 

和方法如何处理Ë触摸UICollectionCell

- (void)collectionView:(UICollectionView *)collectionView 
      didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    // Change the view 
} 

回答

0

你可以委托在UICollectionView的事件DataViewController来处理这些更容易。


1与标签识别您的UICollectionView轻松检索:

//Supposed that your UICollectionView has been declared as an IBOutlet in your .h 
yourCollectionView.tag = 1; 

2 - 添加<UICollectionViewDelegate>DataViewController.h处理UICollectionView事件,

3-设置你浏览的UICollectionView代表在DataViewController之后,您将其添加到mainView

//Replace 1 with the NSInteger you chose 
[[[mainView subviews] lastObject] viewWithTag:1] setDelegate:self]; 

4-处理您的DataViewController上的didSelectItemAtIndexPath以更改您的视图。

0

尝试这样it'l帮助您将其子视图查看你想要的,

[self.view bringSubviewToFront:view]; 
+0

这将加载视图的控制器? – 2013-04-23 12:25:38

+0

我测试了它,它不起作用:/ – 2013-04-23 12:50:29

+0

bringSubviewToFront的参数:方法不是一个字符串,它应该是UIView或它的子类。 – Jeepston 2013-04-23 13:33:24

相关问题