2017-08-15 167 views
0

在我的基於選項卡的應用程序中,我有一個視圖控制器與我在故事板中創建的UITableView。當你滑動表格中的一個圖像時,我希望當前的視圖控制器(SecondViewController)加載一個xib文件(SpeciesViewController.xib),以便「將應用程序」轉換到新視圖。到目前爲止,didSelectRowAtIndexPath被稱爲滑動,但xib文件從不加載。視圖控制器從其他視圖控制器推不會加載

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SpeciesViewController* speciesController = [[[SpeciesViewController alloc]initWithNibName:@"SpeciesViewController" bundle:nil] autorelease]; 

    // SpeciesViewController* speciesController = [[SpeciesViewController alloc] init]; 
    Species *theSpecies = [fetchedResultsController objectAtIndexPath:indexPath]; 
    speciesController.theSpecies = theSpecies; 

    switch (sortBySegmentedControl.selectedSegmentIndex) { 
     case kSortByCommonNameFirst: 
      speciesController.title = [theSpecies commonNameFirstLast]; 
      break; 
     case kSortByCommonNameLast: 
      speciesController.title = [theSpecies commonNameLastFirst]; 
      break; 
     case kSortByScientificName: 
      speciesController.title = [[NSString alloc] initWithFormat:@"%@%@", 
             [theSpecies.scientificName substringToIndex:1], 
             [[theSpecies.scientificName substringFromIndex:1] lowercaseString]]; 
      break; 
     default: 
      break; 
    } 

    speciesController.hidesBottomBarWhenPushed = YES; 
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 

    //// Store current index path for viewDidAppear animation. //// 
    self->currentSelectedIndexPath = indexPath; 

    [self.navigationController pushViewController:speciesController animated:YES]; 

} 

SpeciesViewController筆尖有SpeciesViewController在屬性檢查器中的自定義類。出於這個原因,我期望在I pushViewController:speciesController時調用ViewDidLoad或SpeciesViewController.m的任何其他方法。

很多關於加載筆尖問題的帖子都與initWithNibNameinitWithCoder的錯誤有關。但是,我相信我正確使用initWithNibName,因爲我是從視圖控制器中這樣做的。

我感謝任何幫助!非常感謝!

回答

0

我認爲你不應該使用自動釋放,這應該工作,如果筆尖的名字是正確的

SpeciesViewController* speciesController = [[SpeciesViewController alloc]initWithNibName:@"SpeciesViewController" bundle:nil] ; 

而且我覺得你應該先嵌入內NavigationController你tableviewcontroller,如果你還沒有做到那麼我想自我.navigationControlller目前爲零。

就能呈現視圖控制器這樣

[self presentViewController:speciesController animated:YES completion:nil]; 

希望它可以幫助

+0

有我的方式來裝載從'SecondViewController'筆尖不需要導航控制器實際? – Matt

+0

@Matt是的,你可以呈現視圖控制器,檢查我的更新的答案,如果面臨問題,請 – 3stud1ant3

+0

presentViewController工作 - 謝謝你! – Matt

相關問題