2011-03-24 119 views
1

我不完全確定這樣做的最佳做法,所以我想我會問。帶有嵌套視圖控制器的iOS自定義導航

下面是該程序的目標:

  • 經由根的UIViewController創建的自定義導航控制器,其實際上並不子類的UINavigationController(爲了容易地操縱設計)
  • 對於不同的屏幕嵌套視圖控制器(由操縱主視圖控制器)
  • 每個嵌套視圖控制器都有自己的筆尖文件

目前,我有它的工作ing,但是每個嵌套視圖控制器不是一個視圖控制器,而是一個子類UIView。我覺得這是不好的做法,因爲我以視圖控制器的方式使用這些UIViews,但沒有視圖控制器的功能(即viewDidLoad)。另外,這些UIViews正在採用UIViewController的通常代理方法(它真的引發了紅旗)。

這實際上是不好的做法?

事情恐怕嘗試切換到UIViewControllers的時候是我仍然要做出的UIView的子類,以確定哪些查看指向,當我通過加載筆尖:

NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil]; 

for (id object in bundle) { 
    if ([object isKindOfClass:[SubclassedUIView class]]) 
     currentScreenViewController = (SubclassedUIView *)object; 
} 

我的天堂還沒有檢查,但我假設我必須在該語句中執行「SubclassedUIView」而不僅僅是UIView,因爲該包中還有其他UIView對象。但是再一次,這種情況可能比現在的情況好一些。

不同的解決方案可能是使MainViewController成爲所有需要委託的UIView的委託,併爲每個嵌套的nib創建包含委託方法的MainViewController類別。

這裏的任何想法?

回答

0

很顯然,我在做什麼根據How to add an UIViewController's view as subview有幾分確定。

+0

你也可以檢查解決方案從另一篇文章http://stackoverflow.com/questions/17499391/ios-nested-view-controllers-view-inside-uiviewcontrollers-view – 2015-07-23 10:13:08

3

爲每個SubclassedUIView類型創建一個UIViewController子類,勾選爲接口創建XIB文件的選項,然後將所有代碼移動到該位置,將其指向self.view。然後用該視圖控制器的.xib打開Interface Builder,並儘可能多地配置UIView的外觀。爲了操縱主視圖控制器中的視覺元素,您必須爲每個元素分配「標籤」編號,或創建一大堆實例變量並將它們連接到IB中的元素。你的方法正確使用本視圖控制器一個UINavigationController是:

/* at the top of your main view controller */ 
#import "SubclassedUIViewController.h" 

/* when navigating to the next view */ 
SubclassedUIViewController *newController = [[SubclassedUIViewController alloc] initWithNibName:@"SubclassedUIViewController" bundle:nil]; 
[(UILabel *)[newController.view viewWithTag:6] setText:@"Text"]; // example of accessing elements using tags 
[newController.textLabel setText:@"Text 2"]; // example of accessing elements using IBOutlet connections 
[self.navigationController pushViewController:newController animated:YES]; 
[newController release]; 

Interface Builder中,您還可以導航控制器添加到您的主視圖控制器的.xib文件,並提供了返回按鈕的文字,主標題等。當你實現你的子類視圖控制器,覆蓋initWithNibName:bundle:並設置self.navigationItem.titleself.navigationItem你想要的任何其他屬性。

編輯:你的意思是你必須能夠操縱一些subclassed視圖的特定屬性,而在其他人的?如在,你需要所有的時間訪問它們?如果是這樣,則情況在加載主視圖控制器讓你的子類的意見連接,即:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.subclassedController1 = [[SubclassedUIViewController1 alloc] initWithNibName:@"SubclassedUIViewController1" bundle:nil]; 
    // self.subclassedController2 = ... etc 
} 

/* in your loading next view code you can skip the alloc/init line */ 
[self.navigationController presentViewController:subclassedController2 animated:YES]; 

,然後在子類控制器,你可以到主控制器在幾個方面:

  1. 致電self.parentViewController
  2. 致電[(MyCustomAppDelegateClass *)[[NSApplication sharedApplication] delegate] rootView]或類似(基本上,您的應用程序委託對應用程序的主視圖的引用)。
  3. 通過將@property (assign)屬性和ivar添加到指向主視圖控制器的子類控制器中。您可以使用subclassedController1.mainViewController = self加載主視圖控制器時指定此選項。

幫助文檔:

  1. View Controller Programming Guide for iOS: Navigation Controllers
  2. Xcode Quick Start Guide(更新的Xcode 4)
+0

對不起 - 我不想要UINavigationController的子類,但是,因爲它是限制性的。編輯我的問題有點反映,這是明確的。 – AndrewKS 2011-03-25 15:13:15

+0

上面寫的所有東西都可以由普通的,非子類的'UINavigationController'處理。我談論的子類是你用默認導航控制器推送和彈出的內容。即使根視圖控制器只是一個子類'UIViewController'。導航控制器本身實際上是在主窗口的Interface Builder界面內定義的,作爲根視圖控制器主視圖的子視圖。 – darvids0n 2011-03-28 00:13:26

+0

我想做同樣的事情,但假設UINavigationController不是要走的路,因爲您無法指定下一個控制器如何轉換。例如,push/pop控件從左至右控制它們的動畫,但我想要更多地控制它。 我做了一個可以工作的原型,但是我認爲它在集成到我的代碼庫時遇到了多線程問題,因爲我的原型項目不再工作。我懷疑UINavigationController中有些複雜的東西,UIViewController不知道怎麼做。我不知道。 – horseshoe7 2011-11-24 22:51:03

相關問題