1

我创建了一个自定义 UICollectionViewCell,其中包含多个按钮和标签。我需要每个按钮通过弹出框显示不同的视图。我尝试将 segues 放在按钮上,这给了我编译器错误。

".../MainStoryboard_iPad.storyboard: Couldn't compile connection: <IBCocoaTouchOutletConnection:0x4012ea380  <IBProxyObject: 0x4012d7600> => anchorView => <IBUICollectionViewCell: 0x4012d06a0>>"

我尝试将 segue 放在原型单元本身上,结果相同。我从按钮和标签中删除了所有 IBOutlets,仍然得到编译器错误。

如何正确设置一个集合视图单元到网点和转场?我已经看过做这件事的教程,但它根本不起作用。

蒂亚!

珍妮

4

1 回答 1

1

这对我有用。我希望每个集合视图单元格都有一个会弹出一个弹出窗口的按钮。

首先,在 Interface Builder 中,我在我的故事板中创建了一个新的视图控制器——这个视图控制器是用户触摸按钮时的目标弹出框。在 IB 中,确保在 Identity Inspector 中为该视图控制器提供一个 Storyboard ID,我使用了“destinationView”。在 Attributes Inspector 中,我将 Size 设置为“Form Sheet”,并将 Presentation 设置为“Form Sheet”。

我将按钮放到源视图控制器上我的集合视图中的集合视图单元格上。

在我的源视图控制器中,我创建了一个处理按钮的方法:

- (IBAction)handleButton:(id)sender

然后我在 Interface Builder 的源视图控制器中将该按钮操作与该方法挂钩。

此函数的代码如下所示:

- (IBAction)handleButton:(id)sender
{
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    // get an instance of the destination view controller 
    // you can set any info that needs to be passed here 
    // just use the "sender" variable to find out what view/button was touched
    DestinationViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:@"destinationView"]; 

    // create a navigation controller (so we can have buttons and a title on the title bar of the popover) 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:destVC];

    // say that we want to present it modally 
    [navController setModalPresentationStyle:UIModalPresentationFormSheet];

    // show the popover 
    [[self navigationController] presentViewController:navController animated:YES completion:^{

    }]; 
}

在我的目标视图控制器中,我添加了一个标题和一个用于关闭弹出框的完成按钮:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissExampleButton:)];

    self.title = @"Example Title";
}

- (IBAction)dismissExampleButton:(id)sender
{
    [[self parentViewController] dismissViewControllerAnimated:YES completion:^{

    }]; 
}

请注意,我尝试使用 segues 代替。但是,弹出窗口对我不起作用,它只是动画到一个新的全尺寸目标视图控制器,即使 IB 中的设置是“表单”。我如上所述在 IB 中设置了目标视图控制器,并创建了从源视图控制器到目标视图控制器的 segue。我在属性检查器中给了 segue 一个标识符,比如“exampleSegue”。我将按钮连接到源视图控制器中的操作。

通过这种方式,源视图控制器看起来像:

- (IBAction)handleButton:(id)sender
{
    [self performSegueWithIdentifier:@"exampleSegue" sender:sender];
}

而且,为了向目标视图控制器获取数据,我还在源视图控制器中实现了这一点:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"exampleSegue"]) {

        DestinationViewController *destViewController = (DestinationViewController *)[segue destinationViewController];

        // give the destViewController additional info here
        // destViewController.title.text = @"blah"; 
        // etc. 
    } 
}
于 2013-02-25T10:29:56.237 回答