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?
一切正常,如果我这样做,中心的按钮不再能识别触摸,就像它有一些拦截触摸的东西,这里有什么问题?我错过了什么?
1 个解决方案
#1
1
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
#1
1
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