如何在按下位于 XCode 中带有故事板的自定义 UIView 内的 UIButton 时创建一个 segue
How to create a segue on pressing a UIButton that's locted inside a custom UIView with storyboard in XCode
通常(当使用 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
}
有没有办法在情节提要中执行此操作,还是我可以在代码中执行此操作?
不幸的是,似乎无法通过 Interface Builder 从可设计视图创建 Segue。将可设计视图视为在 Interface Builder 中呈现视图的一种方式,这样它看起来更像您在模拟器或真实设备上获得的视图。
或者,在您的自定义视图 (KachelView
) 上创建一个 IBAction
并委托回拥有它的视图控制器(在设置视图时将视图控制器设置为委托)。然后您可以使用以下代码轻松地以编程方式执行 segue:
performSegue(withIdentifier: <YOUR_IDENTIFIER>, sender: self)
通常(当使用 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
}
有没有办法在情节提要中执行此操作,还是我可以在代码中执行此操作?
不幸的是,似乎无法通过 Interface Builder 从可设计视图创建 Segue。将可设计视图视为在 Interface Builder 中呈现视图的一种方式,这样它看起来更像您在模拟器或真实设备上获得的视图。
或者,在您的自定义视图 (KachelView
) 上创建一个 IBAction
并委托回拥有它的视图控制器(在设置视图时将视图控制器设置为委托)。然后您可以使用以下代码轻松地以编程方式执行 segue:
performSegue(withIdentifier: <YOUR_IDENTIFIER>, sender: self)