首页 > 解决方案 > 在 Obj-C ViewController 上打开 Swift ViewController

问题描述

我有一个 Swift 项目,我正在通过以下方式将 Obj-C ViewController 附加到它的视图:

    + (void)attachGameController:(UIViewController *)view
    {
        GameViewController *c = [GameViewController numberTile:4 backgroundColor:[UIColor whiteColor] buttonControls:NO swipeControls:YES];
        [view addChildViewController:c];
        [view.view addSubview:c.view];
        [c didMoveToParentViewController:view];

        // configure the view
        c.view.translatesAutoresizingMaskIntoConstraints = NO;
        [c.view.topAnchor constraintEqualToAnchor:view.view.topAnchor].active = YES;
        [c.view.leftAnchor constraintEqualToAnchor:view.view.leftAnchor].active = YES;
        [c.view.widthAnchor constraintEqualToAnchor:view.view.widthAnchor].active = YES;
        [c.view.heightAnchor constraintEqualToAnchor:view.view.heightAnchor].active = YES;
    }

// from Swift ViewController I'm calling the above method as
attachGameController(self)

这很好用,包括 Swift 和 Obj-C 视图之间的手势行为!

当我想在上述视图的某个点上显示一个弹出窗口 Swift ViewController 时,问题就开始了。我尝试了以下但都无法打开 ViewController 并且没有错误:

let help = HelpUserController()

self.presentingViewController?.present(help, animated: false, completion: nil)

// or

self.present(help, animated: false, completion: nil)

// or

help.view.frame = self.view.frame
self.addChildViewController(help)
self.view.addSubview(help.view)
help.didMove(toParentViewController: self)

谢谢!

标签: iosobjective-cswift

解决方案


推荐阅读