【问题标题】:Segue instantiated ViewController with instantiateViewControllerWithIdentifier使用 instantiateViewControllerWithIdentifier 对实例化的 ViewController 进行 Segue
【发布时间】:2014-10-14 09:27:07
【问题描述】:

是否可以在使用 instantiateViewControllerWithIdentifier 实例化的 ViewController 的视图中使用 segue?

这是我正在尝试做的一个最小示例(我在更大的项目中需要类似的东西): 在主故事板中,我有一个 rootViewController、一个带有 StoryboardID“secondViewControllerID”的 secondViewController 和一个 thirdViewController。 secondViewController 通过按钮通过 show-Segue 与 thirdViewController 连接。

在 rootViewController 的类中,我实例化了 secondViewController 并将其视图设置为我的 rootViewController 的子视图:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let storyboard = UIStoryboard(name: "Main", bundle: nil);
        var vc = storyboard.instantiateViewControllerWithIdentifier("secondViewControllerID") as UIViewController
        self.view.addSubview(vc.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

当我现在执行程序时,我正确地看到了子视图(secondViewController)。但是现在 segue 不再起作用(当我单击按钮时,我没有到达 thirdViewController)

有人知道为什么吗?

(在我的项目中,不可能只使用没有 instantiateViewControllerWithIdentifier 的 secondViewController,因为 secondViewController 是 pageViewController 的一部分,它通过 instantiateViewControllerWithIdentifier 管理其 viewControllers)

【问题讨论】:

标签: ios swift segue viewcontroller


【解决方案1】:

您将一个视图控制器的视图添加为另一个视图控制器的子视图。但默认情况下,点击和手势由您的主视图控制器处理,不会传递给辅助 VC - 因此是您的问题。您需要调用一些函数才能使其正常工作。查看 Apple Docs here

查看“创建自定义容器视图控制器”部分。总之,您需要用self.addChildViewController(vc)vc.didMoveToParentViewController(self) 函数调用来包装您的addSubview 方法调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2020-09-03
    • 2020-04-28
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    相关资源
    最近更新 更多