【问题标题】:How to create a segue on pressing a UIButton that's locted inside a custom UIView with storyboard in XCode如何在按下位于 XCode 中带有情节提要的自定义 UIView 内的 UIButton 时创建一个 segue
【发布时间】:2017-09-20 20:28:29
【问题描述】:

通常(当使用 XCode 故事板创建 UI 时)ctrl-drag 会创建到另一个视图控制器的 segue。与 UIButton 完美搭配。

但是如果 UIButton 位于自定义视图(即 UIView 的子类)内,如何使用 ctrl-drag 创建一个 segue?

import UIKit

// class KachelView is used several times in 
// the main storyboard on entry panel.

@IBDesignable
class KachelView: UIView {
// normal stuff here
// XIB file contains a UIButton that is 
// to ignite a segue in the outer view
}

有没有办法在情节提要中做到这一点,还是我应该在代码中做到这一点?

【问题讨论】:

  • 你不能在情节提要中从你想要的按钮上按住 ctrl-drag 吗?
  • UIButton 是自定义 UIView 的一部分。这似乎可以防止将它用作普通的 UIButton 和 ctrl-drag 的东西。不工作
  • 使用@Edgar 的答案加上一些额外的麻烦解决了问题。我从 UIControl 继承而不是 UIView。所以我可以使用 UIKit 的通知器/观察者机制在 KachelView 中创建一个可以在故事板类中订阅的事件。然后按代码继续。

标签: swift xcode storyboard segue custom-view


【解决方案1】:

不幸的是,似乎无法通过 Interface Builder 从 Designable View 创建 Segue。将 Designable View 视为在 Interface Builder 中呈现视图的一种方式,使其看起来更像您在模拟器或真实设备上看到的。

或者,在您的自定义视图 (KachelView) 上创建一个 IBAction 并将其委托给拥有它的视图控制器(在设置视图时将视图控制器设置为委托)。然后,您可以使用以下方式轻松地以编程方式执行 segue:

performSegue(withIdentifier: <YOUR_IDENTIFIER>, sender: self)

【讨论】:

    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 2014-02-07
    • 2012-07-22
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2017-10-19
    • 2012-03-08
    相关资源
    最近更新 更多