简体   繁体   English

使用Swift,如何使用StoryBoards在另一个视图上创建一个UIViewController

[英]Using Swift, how to create a UIViewController over another one using StoryBoards

I have a viewcontroller that hold a button for now, and I want to create a new viewcontroller and add his view over the current one 我有一个现在按住按钮的viewcontroller,我想创建一个新的viewcontroller并在当前视图上添加他的视图

So far I did this: 到目前为止,我做到了:

class ViewController : UIViewController
{
    var myController: MyController = MyController.controller()

    init(coder aDecoder: NSCoder!)
    {
        super.init(coder: aDecoder)
    }

    init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)
    {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.view.addSubview(self.myController.view)
    }

    @IBAction func buttonPressed(sender : AnyObject)
    {

    }
}

in MyController I did a singleton (I hope I did it right) 在MyController中,我做了一个单例(我希望我做对了)

class MyController : UIViewController
{

    init(coder aDecoder: NSCoder!)
    {
        super.init(coder: aDecoder)
    }

    init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)
    {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    class func controller() -> MyController
    {
        var once : dispatch_once_t = 0
        var sharedInstance: MyController?

        dispatch_once(&once, { sharedInstance = MyController()})

        return sharedInstance!
    }
}

the behavior I got is this, if I DON'T call the 我的行为就是这样,如果我不打电话给

self.view.addSubview(self.myController.view) self.view.addSubview(self.myController.view)

everything works fine, if I did, the button in the center is not anymore recognizing the touches, it is like it has something over that intercept the touch, what is wrong here? 一切正常,如果我这样做了,那么中心的按钮不再能够识别触摸,就好像它有东西挡住了触摸,这是怎么回事? what I'm missing? 我想念的是什么?

in MyController I did a singleton (I hope I did it right) 在MyController中,我做了一个单例(我希望我做对了)

You did it wrong. 你做错了。 Singleton is not required here. 这里不需要单例。 If you are using storyboard, the below line 如果您使用情节提要,则以下行

var myController: MyController = MyController.controller()  

could be 可能

var myController: MyController = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController")

Then before adding sub view set the frame of the self.myController.view 然后在添加子视图之前,设置self.myController.view的框架

self.myController.view.frame = CGRectMake(0, 0, 320, 200); // or what ever frame you want

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:UIViewController不会使用故事板显示另一个UIViewController - iOS: UIViewController doesn't display another UIViewController using storyboards 不使用故事板时使用 Swift 初始化 UIViewController 的正确方法是什么? - What is the proper way to initialize a UIViewController using Swift when not using Storyboards? 使用Swift Programmaticaly将UIViewController与另一个UIViewController重叠 - Overlapping UIViewController with another UIViewController Using Swift Programmaticaly 在多个情节提要板中使用相同的UIViewController - Using same UIViewController in Multiple Storyboards 是否可以使用故事板使用UIViewController而不是UITableViewController创建静态表? - Is it possible to use storyboards to create a static table using UIViewController instead of UITableViewController? 在另一个UIViewController中使用一个UIViewController中的UIView? - Using an UIView from one UIViewController in another UIViewController? 如何使用Swift在AppDelegate中导入UIViewController - How to import a UIViewController in AppDelegate using swift 如何使用Storyboard在另一个Controller中子视图UITableViewController - How to subview a UITableViewController within another Controller using Storyboards 使用Swift打开新的UIViewController - Open new UIViewController using Swift 使用当前模式将数据从一个 uiviewcontroller 发送到另一个导航控制器 - Sending data from one uiviewcontroller to another navigationcontroller using present modally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM