使用子视图中的segue

时间:2013-01-05 18:37:32

标签: iphone ios6 segue uicollectionview uistoryboardsegue

我有一个包含集合视图的视图。我以编程方式(在viewDidLoad中)添加了此集合视图 - 因此不在故事板上。此集合视图包含多个单元格。当用户点击集合视图中的一个单元格时,我想要转到另一个视图控制器(我打算在故事板上添加)。我的问题是 - 由于集合视图不在故事板上,我怎么能摆脱它?或者还有其他方法可以实现这一目标。

上述原始问题的一些更新:

在Parent View Controller中,我执行以下操作:

//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?

BTW,Taseen在下面提出了一个选项。我试过了,它正在发挥作用。但据我所知,它实际上并不是“塞浦路斯”。

2 个答案:

答案 0 :(得分:2)

您能否告诉我们您编写的任何代码

我从您的问题中了解到,您已在 viewDidLoad。中添加了集合视图,并且我相信您已将集合视图的委托设置为self,因此在 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 DestinationController 的故事板上创建的segue将具有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"];
}

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

答案 1 :(得分:0)

通过按住Ctrl键单击初始viewController并拖动到目标viewController来创建一个新的segue。不要忘记设置segue标识符。 比覆盖 - (void)performSegueWithIdentifier:(NSString *)标识符sender:(id)sender方法。

相关问题