xcode - UIButton 触发 Segue

标签 xcode swift uibutton segue

我正在尝试将 2 个 Controller 与 segue 连接在一起。我有一个带有 6 个按钮的 View Controller ,单击按钮后将显示一个 tableviewcontroller。我制作了所有链接,给出了用于继续的标识符等。

tableviewcontroller中有一个数组,它应该根据哪个按钮触发segue来显示过滤结果

我应该如何编写prepareforsegue方法?

这就是我正在做的事情

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "chooseBrand" {

        if sender?.tag == 1 {
            let destinationController = segue.destinationViewController as! SelectionTableViewController
            let numberOfCameras = destinationController.Camera.count 

}

问题是我收到此错误消息

"Could not cast value of type 'UINavigationController' (0x10a0ef588) to 'myApp.SelectionTableViewController' (0x108658b90)."

如何根据按下的按钮定义 selectiontableviewcontrollertableview 的内容?

非常感谢您的帮助:)

最佳答案

这里的发件人到底是什么?该错误表明 tag = 1 的发送者不是 SelectionTableViewController。

无论如何,您可以做的只是将每个按钮连接到相同的操作导出方法,并在两个 Controller 之间有一个通用的segue。当您点击按钮 A、B、C、D、E 或 F 时,您只需检查它们的标签(或将它们存储为 Controller 类中的变量)并将您想要的任何内容发送到 Controller 。重要的是, Controller 之间的 Segue 是在两个 Controller 之间,而不是每个 UIButton 与另一个 Controller 之间的 segue。在下面的示例中,我假设您将六个 UIButton 中的每一个存储为类变量。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "segueFromFirstToSecond" {
        let destinationController = segue.destinationViewController as!   SelectionTableViewController
        if sender = button1 {
            let numberOfCameras = destinationController.Camera.count
        } else if sender == button2 {
            let numberOfCameras = destinationController.Camera.count
        }          
}

@IBAction func buttonPressed(sender: AnyObject) {
    performSegueWithIdentifier(segueProfileToPul, sender: sender)
}

将所有按钮连接到buttonPressed()。我认为这行 let numberOfCameras = destinationController.Camera.count 不起作用,因为 SelectionTableViewController 尚未实例化,因此其变量中没有值(我认为)。您正在寻找的可能是设置此值,以便destinationController.Camera.count = someValue。也许我错了。

编辑 我上传了一个测试项目来说明我的意思。注意我在 Storyboard中建立的联系。下载here

关于xcode - UIButton 触发 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719970/

相关文章:

iphone - xcodebuild - 如何定义预处理器宏?

ios - 无法为签名者“iPhone Developer 建立到自签名根的链

ios - 识别 subview 的类

ios - 在同一选项卡上的三个 UIButton 之间切换 selectedState

ios - 在 Swift 中单击按钮而不释放触摸

ios - 使用 Launch Screen.xib 模板的真正通用的 iOS 启动屏幕?

xcode - xcode 6 中的 EXC_BREAKPOINT

ios - 删除 PHAssetCollection (Swift)

ios - 当您复制粘贴时,xib View 内的按钮/文本字段没有反应?

ios - UIButton 从不触发 Action 中的代码