![](/img/trans.png)
[英]iOS: UIViewController doesn't display another UIViewController using storyboards
[英]Using Swift, how to create a UIViewController over another one using StoryBoards
我有一個現在按住按鈕的viewcontroller,我想創建一個新的viewcontroller並在當前視圖上添加他的視圖
到目前為止,我做到了:
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)
{
}
}
在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!
}
}
我的行為就是這樣,如果我不打電話給
self.view.addSubview(self.myController.view)
一切正常,如果我這樣做了,那么中心的按鈕不再能夠識別觸摸,就好像它有東西擋住了觸摸,這是怎么回事? 我想念的是什么?
在MyController中,我做了一個單例(我希望我做對了)
你做錯了。 這里不需要單例。 如果您使用情節提要,則以下行
var myController: MyController = MyController.controller()
可能
var myController: MyController = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController")
然后在添加子視圖之前,設置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.