簡體   English   中英

我只能使UINavigationController在第二級加載,而不能在Root View Controller加載

[英]I can make UINavigationController load only at 2nd level, not at Root View Controller

我試圖尋找類似的問題,但找不到類似的問題。

我正在UIView中加載UINavigationController,而UIView不是(在大多數示例中)MainWindow。

我創建了一個名為DocumentsViewController的新.xib,它是UIView的子類(它具有相關的.m和.h文件)。 我創建了DocumentsRootViewController.xib,它是UITableViewController的子類,應該是UINavigationController的RootViewController。

我移至DocumentsViewController並在Interface Builder中添加了UINavigationController對象。 然后我去編寫代碼,並將其添加到IBOutlet中,然后將其連接到該對象。

在ViewDidLoad中,我執行以下幾行:

DocumentsRootViewController *rootViewController = [[[DocumentsRootViewController alloc] init] autorelease];
rootViewController.title = @"Documents";
[navigationControllerDocuments initWithRootViewController:rootViewController];
[self.view addSubview:navigationControllerDocuments.view];    

它按預期顯示了該表,但顯示了“ Root View Controller”的“返回”按鈕(如下圖所示)。

為什么? 它不應該已經知道已經設置了rootviewcontroller嗎?

預先感謝您澄清這一疑問的人

喬萬尼

UINavigationController行為Xib結構

當您通過Nib添加UINavigationController時,它實際上在nib文件內創建一個UINavigationController的實例,該實例具有默認的RootViewController集(類型為UIViewController)和默認的RootViewController標題。

加載筆尖時,將在加載筆尖時創建該對象(即,在初始化DocumentsViewController時)-因此,navigationControllerDocuments出口已被初始化為UINavigationController,並且已在其中設置了默認ViewController。

我認為正在發生的是,當您調用“ initWithRootViewController”時-您在已初始化的對象上調用了它-因此它再次運行初始化代碼-將第二個視圖控制器(DocumentRootViewController)推入堆棧,但是默認的那個被創建在筆尖已經存在。

您可能應該做的就是忘記在筆尖中創建一個並以編程方式初始化整個事情。

即您在哪里:

[navigationControllerDocuments initWithRootViewController:rootViewController];

我建議您改為執行alloc和init:

[[navigationControllerDocuments alloc] initWithRootViewController:rootViewController];

由於您正在執行此操作,因此您實際上不需要將導航控制器添加到筆尖中,因此,如果這可行,則應將其從筆尖中刪除,因為您將其替換為代碼中的那個。

暫無
暫無

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

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