2013-04-23 80 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

相關問題