创建一个可以触发 segues 的 UIView 子类

Creating a UIView subclass that can trigger segues

UIButton、UITableViewCell 等的实例可用作 Interface Builder 中的 segue 源(即,您可以按住 Ctrl 键并单击这些对象,然后 link 将它们添加到情节提要中的控制器)。

我们可以创建一个直接的 UIView 子类来实现相同的功能吗?如果可以,怎么做?

注意:这里有另一种方式来问我的问题:如何在我自己的 类 的 "Triggered Segues" 部分中添加项目?以下是 UITableViewCell 的内容:

是的,从视图控制器到视图控制器在故事板中创建一个 segue。你必须给那个 segue 一个标识符。然后在该视图上开始监听触摸并调用 [self performSegueWithIdentifier@"YOUR IDENTIFER"];

不,你不能直接让 UIVIEW 有一个动作。你可以添加一个按钮 在这里添加一个按钮来查看

UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[view setUserInteractionEnabled:NO];
[view setBackgroundColor:[UIColor redColor]];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:view];
[btn setFrame:CGRectMake(0, 0, 200, 100)];
[btn addTarget:self action:@selector(click)forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

或添加点击识别器以查看

//The setup code (in viewDidLoad in your view controller)
     UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                      action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

 //Do stuff here...
}

您可以子class 更适合您目的的控件。

在 Xcode 的后面,您必须将 class 的名称放在正确的检查器中,如果它需要不同的选项,请使用键属性。

select编辑 UIView 后,选项卡的工具提示为 'Show identity inspector'。

然后您可以访问自定义 Class 区域,您可以 select class 名称。

一个更复杂的功能是使用 'User Defined Runtime Attributes'

因为我喜欢拥有与 Jean 相同的功能,所以我很困惑。免责声明:我完全是 iOS 和 Apple 的菜鸟,所以我想这一定是我的错,我不明白为什么它不起作用。

这个'answer'更像是一个理论,也许是一个非常糟糕的理论。所以请让我知道是否已经有解决方案。

理论

我认为仅通过代码 extend/control 'Triggered Segues' 部分是不可能的。似乎 XCode 根据从对象库中选择的控件知道触发了要显示的 Segues。

想法

只有一个子class例如UIButton 没有让我在 Connections Inspector 中看到 Triggered Segues 部分。在自定义 Class 部分中设置 class 也无济于事。

只有当我使用对象库中的按钮时,我才会得到触发的 Segues 部分。例如。如果我从 UIButton subclass,我应该使用 'Button'.

我在 Main.storyboard 中使用了 XML - 每当我将 <subviews><button ... 更改为 <subviews><view ... 时,'Triggered Segues' 部分就消失了。把它改回来,它就会出现。见下文。我将包含 customClass="IconButton" 属性的元素从按钮更改为视图。

按钮:已触发 Segues

<viewController title="..." id="Zzf-WE-vxI" customClass="MyCustomClass" ... >
    <view key="view" contentMode="scaleToFill" id="Mfn-O2-CMJ">
        <!-- ... -->
        <subviews>
            <button customClass="IconButton" customModule="...">
                <!-- ... -->
            </button>

视图:没有触发的 Segues

<viewController title="..." id="Zzf-WE-vxI" customClass="MyCustomClass" ... >
    <view key="view" contentMode="scaleToFill" id="Mfn-O2-CMJ">
        <!-- ... -->
        <subviews>
            <view customClass="IconButton" customModule="...">
                <!-- ... -->
            </view>

结论

我的结论是 XCode 显示基于故事板 XML 中的 element/control 的 Triggered Segues 部分 - 它不依赖于 class I subclass 来自。所以我怀疑是否可以通过代码控制 Triggered Segues 部分的内容。

如果我错了请告诉我。如前所述,我现在对 XCode 的内部结构没有真正的经验,我很好奇我的理论是否只是 bla bla。