2012-02-25 100 views
1

我想創建一個使用故事板的UITableView,但我來到的東西可能很簡單,但我不知道如何解決它。 首先讓我指出,我知道故事板的一個侷限性在於,您將不得不挖掘故事板以查找有關您所擁有的視圖的信息並將其鏈接到應用程序代理。 我已經創建了我的可變數組和我將在應用程序委託的表中使用的信息,現在我想將該UITableView引用到應用程序委託。層次結構是這樣說參考視圖使用故事板的應用程序委託

  1. 首先我有根認爲,一旦你點擊一個按鈕,它會重定向到第二視圖
  2. 第二視圖裏面還有另外一個,一旦你按下它,它就會按鈕將您重定向到UINavigationController
  3. UINavigationController包含UITableView。

因此,您可以看到在導航控件和UITableView之前有兩個視圖。

這裏是我嘗試使用的代碼,但它不工作

UIViewController *viewController = (UIViewController *)self.window.rootviewController; 

// The next line refers to the second view but does not work at all 
UIViewController *secondView = [[UIViewController viewController] objectAtIndex:1]; 

//Then the following line is to redirect from the second view to the navigation controller 
UINavigationController *navigationController =[[secondView viewController] objectAtIndex:0]; 

//Then is the table view 
BuildingsViewController *buildingsViewController = [[navigationController viewControllers] objectAtIndex:0]; 

上面的代碼不起作用。誰能幫幫我嗎? 非常感謝

回答

2

如果此代碼位於應用程序委託中,有很多原因可能導致其無法工作。首先,你似乎將View,ViewControllers和Navigation控制器與你正在做的事情混合在一起。其次,當你試圖做到這一點時,並不能保證所有的視圖/視圖控制器還沒有被創建,或者在最終的建築視圖控制器被渲染時以他們的方式連接。

什麼你可以嘗試,而不是在你的BuildingsViewController(你說的是你的表視圖控制器),您可以通過使用

MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate 

獲取句柄到App代表一旦你有一個手柄,委託你可以簡單地引用您在您的BuildingsViewController中創建的可變數組結構等。

例如在 'numberOfRowsInSection' 的方法:

MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate 
NSMutableArray *myBuildings = myAppDelegate.buildingArray; 
return [myBuildings count]; 

或者cellForRowAtIndexPath方法中:

// something like this but using your names for app delegate, building array and the accessor for the building name 
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate 
NSMutableArray *myBuildings = myAppDelegate.buildingArray; 

cell.textLabel.text = [myBuildings objectAtIndex indexPath.row].theBuildingName; 
+0

您好,感謝您的答覆。 我做了你所說的並且只爲numberOfRowsInSection工作。對於cellForRowAtIndexPath有一些錯誤,根本不起作用 – user1015777 2012-02-26 10:50:49

+0

什麼部分不起作用?關鍵點是演示如何通過應用程序上的單例訪問器訪問包含表格視圖控制器中的表格模型數據的數組,以訪問應用程序委託。 – gamozzii 2012-02-26 23:40:14

相關問題