[英]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.