簡體   English   中英

嵌套的UIViewController需要推送到根UINavigationController

[英]Nested UIViewController needs to push onto root UINavigationController

我有一個帶有UINavigationController的應用程序,它包含一個根UIViewController子類(PagingViewController),它管理並充當水平分頁UIScrollView的委托。 在這個UIScrollView的每個頁面上,都有一個垂直滾動的UITableView,每個都由它自己的UIViewController子類(PageViewController)管理。 它看起來像這樣......

UINavigationController
  PagingViewController (root VC of navigation controller)
    PageViewController (instance 1)
    PageViewController (instance 2)
    PageViewController (instance 3)

  HowToPushThisViewController (this is what I'd like to push onto the current root, PagingViewController, from one of its sub view controllers)

我想做的是,在PageViewController(UITableView委托)的didSelectRowAtIndexPath:方法中,將新的UIViewController推送到UINavigationController(在PagingViewController之上)。

問題是PageViewController的navigationController屬性是nil,所以我不能把它推到那里。 我想這是零,因為它不是直接的UINavigationController的堆棧,而是由PagingViewController這在棧上 我嘗試在將每個PageViewController添加到PagingScrollView之前設置它的navigationController屬性,但它似乎只是只讀(UINavigationController如何設置自己?)。

我覺得嵌套的UIViewControllers是健全的架構(每個都需要管理並充當其視圖的委托),並且想要從子控制器推送導航是合理的,所以我不確定訪問和訪問的最佳實踐從嵌套控制器推出。 希望有一些簡單的東西我可以忽略訪問並推送到根導航控制器。

合乎邏輯的做法是設置頁面視圖控制器的parentViewController屬性,但是它也是只讀的。 Apple不鼓勵視圖控制器的自定義層次結構(除了帶有標簽欄,導航和拆分視圖控制器的受支持層次結構)。

在您的情況下,我將在PageViewController類上定義一個自定義的parentVC屬性, PagingViewController在實例化其子對象時將其設置為自身。 然后, PageViewController可以通過導航控制器進入

self.parentVC.navigationController

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM