2013-01-05 84 views
0

我有包含集合視圖的視圖。我以編程方式添加了這個集合視圖(在viewDidLoad中) - 所以這不在故事板上。此收集視圖包含多個單元格。當用戶點擊集合視圖中的一個單元格時,我想繼續使用不同的視圖控制器(我打算在故事板上添加)。我的問題是 - 由於收集視圖不在故事板上,所以我怎麼能從中解脫出來?或者還有其他方法可以實現這一點。從子視圖中使用segue

某些更新上面的原題:

在父視圖控制器,我做了以下內容:

//Layout for the collection view 
CDLayout *cdLayout = [[CDLayout alloc] initWithItemSize:CGSizeMake(cardWidth, cardHeight) withItemInsets:UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset) withInterItemSpacingX:8.0f withTopMargin:margin withLeftMargin:margin withBottomMargin:margin withRightMargin:margin shouldRotate:NO]; 

//This is the collection view controller 
CDLineLayoutViewController *cdLineVC = [[CDLineLayoutViewController alloc] initWithCollectionViewLayout:cdLayout withItemCount:12 ]; 

// add the collection view controller to self - the parent  
[self addChildViewController:cdLineVC]; 

[cdLineVC didMoveToParentViewController:self]; 

// add collectionView as a subview 
[self.view addSubview:cdLineVC.collectionView]; 

的的CollectionView有12卡。當用戶點擊我想要移動到另一個視圖控制器的卡片之一時。

正如你所看到的集合視圖不在故事板上。那麼,有沒有辦法創建一個segue?

順便說一句,一個選項是Taseen在下面提出的建議。我試過了,它正在工作。但據我所知,它實際上並不是一個「繼續」。

回答

0

通過按住Ctrl並點擊初始viewController並拖動到目標viewController來創建一個新的segue。不要忘記設置segue標識符。 比覆蓋 - (void)performSegueWithIdentifier:(NSString *)標識符發件人:(id)發件人方法。

+0

我將如何識別集合視圖中的哪個項目被選中(或點擊)?不應該有一種方法來處理子視圖(在這種情況下,集合視圖)控制器中的segue。 –

+0

作爲performSegueWith ...方法的第二個參數的sender對象是tapped對象。每個視圖對象都有NSInteger屬性_tag_。指定一個唯一編號的視圖對象。用這個數字你可以識別被點擊的視圖對象。 –

+0

可能是簡單的方法是不使用segues。 @Taseen解決方案是更好的方法。 –

2

能否請你告訴我們,你已經寫

我從你的問題理解的是,你已經在viewDidLoad中添加集合視圖的任何代碼。我相信你已經設置的收集以自我的代表,所以在 didSelectItemAtIndexPath方法,你可以這樣寫代碼

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //From indexpath.item, you know which cell was clicked 

    //using switch method 

    switch (indexPath.item) { 
     case 0: 
      //You Can push the view controller from here 
      //You need to import the header file of that View controller 
      DestinationViewController *destinationView; 

      //if the self controller in which the collection view is shown is embedded in Navigation controller, 
      //you can push using 
      [self.navigationController pushViewController:destinationView animated:YES]; 

      //if it is not embedded, use modal segue 
      [self.modalViewController presentModalViewController:destinationView animated:YES]; 


      break; 

     default: 
      break; 
    } 
} 

編輯:你將在故事板創建您的ParentViewController的SEGUEDestinationController將具有segueIdentifier屬性。像顯示在下面 enter image description here 然後在didSelectItemAtIndexPath代替推控制器,你可以使用此代碼

[self performSegueWithIdentifier:@"collectionViewSegue" sender:self]; 

,還可以配置使用prepareForSegue方法目的地視圖控制器..

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    DestinationViewController *targetVC = (DestinationViewCOntroller *)segue.destinationViewController; 
    //if you pass anything you can do it here 
    //if to set any public variable for example image for the imageview 
    targetVC.cardImageView.image = [UIImage imageNamed:@"queen.png"]; 
} 

此方法將在您的父母控制器中。

+0

我在問題中添加了代碼。 –

+0

另外,我做了你的建議,它正在工作。 –

+0

我編輯了我的答案 – Taseen