[英]Unresponsive view instantiated with instantiateViewControllerWithIdentifier:
[英]Segue instantiated ViewController with instantiateViewControllerWithIdentifier
是否可以在由InstantiateViewControllerWithIdentifier實例化的ViewControllers視圖中使用segues?
這是我要執行的操作的一個最小示例(在較大的項目中,我需要類似的內容):在主Storyboard中,我有一個rootViewController,一個帶有StoryboardID“ secondViewControllerID”的secondViewController和thirdViewController。 通過show-Segue通過按鈕將secondViewController與thirdViewController連接。
在rootViewController的類中,我實例化secondViewController並將其視圖設置為rootViewController的子視圖:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let storyboard = UIStoryboard(name: "Main", bundle: nil);
var vc = storyboard.instantiateViewControllerWithIdentifier("secondViewControllerID") as UIViewController
self.view.addSubview(vc.view)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
現在執行程序時,我可以正確看到子視圖(secondViewController)。 但是現在segue不再起作用了(當我單擊按鈕時,我沒有進入thirdViewController)
有誰知道為什么?
(在我的項目中,僅在沒有InstantiateViewControllerWithIdentifier的情況下使用secondViewController是不可能的,因為secondViewController是pageViewController的一部分,該頁面正在通過InstantiateViewControllerWithIdentifier管理其viewController)
您正在將一個視圖控制器的視圖添加為另一視圖控制器的子視圖。 但是默認情況下,輕擊和手勢由您的主視圖控制器處理,並且不會傳遞給輔助VC-因此會出現問題。 您需要調用一些函數才能使其正常工作。 在這里看看Apple Docs。
查看“創建自定義容器視圖控制器”部分。 總之,您需要使用self.addChildViewController(vc)
和vc.didMoveToParentViewController(self)
函數調用包裝addSubview
方法調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.