【问题标题】:View Controller Pushed from other view controller never loads从其他视图控制器推送的视图控制器永远不会加载
【发布时间】:2017-08-15 04:11:40
【问题描述】:

在我的基于选项卡的应用程序中,我有一个带有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 nib 在属性检查器中将SpeciesViewController 作为其自定义类。出于这个原因,我希望 ViewDidLoad 或 SpeciesViewController.m 中的任何其他方法在我 pushViewController:speciesController 时被调用。

许多关于加载笔尖问题的帖子都与initWithNibNameinitWithCoder 的错误有关。但是,我相信我正确地使用了initWithNibName,因为我是从视图控制器中这样做的。

感谢您的帮助!非常感谢!

【问题讨论】:

    标签: ios objective-c uiviewcontroller nib


    【解决方案1】:

    我认为你不应该使用自动释放,如果笔尖名称正确,这应该可以工作

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

    而且我认为你应该首先将你的 tableviewcontroller 嵌入到 NavigationController 中,如果你还没有这样做,那么我猜 self.navigationController 目前是 nil。

    你可以像这样展示视图控制器

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

    希望对你有帮助

    【讨论】:

    • 有没有办法让我从SecondViewController 加载笔尖,实际上不需要导航控制器?
    • @Matt 是的,您可以展示视图控制器,查看我更新的答案,如果遇到问题,请询问
    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多