2012-02-24 81 views
0

剛開始iOS開發時,我得到了在選項卡1(第二個選項卡)中使用TableViewController創建UITabBarController時的崩潰。iOS UITabBarController問題

這裏是我的代碼 - AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
diseases = [NSMutableArray arrayWithCapacity:5]; 
     Disease *disease1 = [[Disease alloc] init]; 
     Disease *disease2 = [[Disease alloc] init]; 
     Disease *disease3 = [[Disease alloc] init]; 
     Disease *disease4 = [[Disease alloc] init]; 
     Disease *disease5 = [[Disease alloc] init]; 

     disease1.name = @"disease1"; 
     disease2.name = @"disease2"; 
     disease3.name = @"disease3"; 
     disease4.name = @"disease4"; 
     disease5.name = @"disease5"; 

     [diseases addObject:disease1]; 
     [diseases addObject:disease2]; 
     [diseases addObject:disease3]; 
     [diseases addObject:disease4]; 
     [diseases addObject:disease5]; 


UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 

UINavigationController *navigationController = 
[[tabBarController viewControllers] objectAtIndex:0]; 

FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"NormalBiopsy" bundle:nil]; 
firstViewController = [[tabBarController viewControllers] objectAtIndex:0]; 

DiseaseTableViewController *diseaseTableViewController = [[DiseaseTableViewController alloc] initWithNibName:@"DiseaseTableViewController" bundle:nil]; 
diseaseTableViewController = [[tabBarController viewControllers] objectAtIndex:1]; 

SecondViewController *secondViewController = [[SecondViewController alloc] 
               initWithNibName:@"TestYourSelf" bundle:nil]; 
secondViewController = [[tabBarController viewControllers] objectAtIndex:2]; 

ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"About" bundle:nil]; 
thirdViewController = [[tabBarController viewControllers] objectAtIndex:3]; 

// Override point for customization after application launch. 
return YES; 
} 

這導致與在DiseaseTableViewController的TableView中空白表的UITabBarController - 因爲填充任何數據。 但是,如果我做

diseaseTableViewController.diseases = diseases; 

應用程序崩潰與:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-    [UINavigationController setDiseases:]: unrecognized selector sent to instance 

於是,我就添加diseaseTableViewController到navigationController:

diseaseTableViewController = [[navigationController viewControllers] objectAtIndex:1]; 

導致:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstViewController viewControllers]: unrecognized selector sent to instance 

我嘗試在該類實現中填充DiseaseTableViewController而不是AppDelegate,但在UI中再次以空白表格結束。

任何想法?

回答

0

我解決了這個由離開原來的AppDelegate爲是,而在viewDidLoad中填充DiseaseController.diseases的數據()有點像什麼@Zhang提到

0

嗯,不知道我是否正確理解你的問題,但基本上從我的理解你想創建一個帶有標籤欄界面的應用程序,每個標籤都可以有自己的導航?

你可能想是這樣的應用程序結構:

AppDelegate 
| 
| 
|___ UITabBarController 
    | 
    |__ UINavigationController1 
    | | 
    | |__ DiseaseController1 
    | 
    | 
    | 
    |__ UINavigationController2 
    | | 
    | |__ DiseaseController2 
    | 
    | 
    | 
    |__ UINavigationController3 
    | | 
    | |__ DiseaseController3 
    | 
    | 
    | 
    | 
    |__ UINavigationController4 
    | | 
    | |__ DiseaseController4 
    | 
    | 
    | 
    | 
    |__ UINavigationController5 
     | 
     |__ DiseaseController5 

所以下面這個結構,你會:

  1. 創建和設置您的UITabBarController,你根本控制器爲AppDelegate中
  2. 創建您的UINavigationController將它們設置爲您的UITabBarController的每個選項卡
  3. 將匹配的DiseaseViewController添加到ea你的UINavigationController的

當你處理表時,你需要記住設置表的數據源和委託。那麼,哪曾DiseaseViewController您在創建表

,你需要做的是這樣的:

myTable.source = self; 
myTable.delegate = self; 

凡自指DiseaseViewController您創建的表中如

// psuedocode 
@class Disease1 
{ 
    UITableView myTable; 
} 

@property (nonatomic, retain) UITableView myTable; 

@end 

// implementation code 
@implementation Disease1 

-(void)viewDidLoad() 
{ 
    . . . 
    myTable.source = self; 
    myTable.delegate = self; 
    . . . 
} 

@end 
+0

謝謝,但我想你稍稍偏出了來自我可憐的解釋的問題!只有DiseaseTableViewController(有一系列疾病)在標籤1(第二個標籤)上,其餘的是不同的視圖控制器。因此,我會有5種疾病作爲表中的條目(這是否有意義,或者我可以在DiseaseTableViewController的viewDidLoad方法或它的視圖?)。我已經有一個UiNavigationController指向DiseasTableViewController,但不斷得到該選擇錯誤 – oliveromahony 2012-02-24 19:16:31

相關問題